Constants in C++ Programming


  • Constants are called as Literals.
  • They are like variables, except that their value never changes during execution once defined.
  • The 'const' keyword is used to define constraints in C++.

  • Syntax:
    const datatype constant_name;

    const int a = 10;

  • It is possible to put const either before or after the data type.

  • Example:
    int const a = 10;


Following are the types of Literals:

Integer LiteralIt can be a decimal, octal or hexadecimal constant.
Floating-point LiteralIt has an integral part, a decimal point, a fractional part and an exponent part.
Boolean LiteralIt has two boolean literals: True and False.
Character LiteralThese literals are enclosed in single quote.
String LiteralThese literals are enclosed in double quotes.

Escape Codes

  • Escape codes are used to present the characters which are difficult to express in the source code.
  • All escape codes are start with a blackslash (\).
Escape CodesMeaning
\rCarriage return
\vVertical tab
\fForm feed
\'Single quote
\"Double quote
\?Question mark