Constants in C++ Programming

Constants

  • 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;

    Example:
    const int a = 10;

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

  • Example:
    int const a = 10;

Literals

Following are the types of Literals:

LiteralsDescription
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
\nNewline
\rCarriage return
\tTab
\vVertical tab
\bBackspace
\fForm feed
\aAlert
\'Single quote
\"Double quote
\?Question mark
\\Backslash