C++ Constants


What are Constants?

The constants can be referred as fixed values that the program cannot change, it cannot be altered. The constants are also known as Literals. They are initialized at the time of creation and new values cannot be assigned later on. It can be of any data type and are divided into integer, floating point, Boolean, characters and strings. They are treated as regular expressions but their values cannot be modified.

Let us have a look at different literals

Integer Literals

The integer literal is identified as integer values. They can be decimal, octal and hexadecimal constants. As they are digits, so, they are not enclosed in quotes or any other special characters which represents the whole number in decimal base. In integer literals the prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal but nothing for decimal. Some of the examples are 57 as decimal, 0213 as octal, 0x4b as hexadecimal.

Floating Point Literals

The floating point literals are used to express real values with decimals. The floating point literals consist of integer part, decimal point, a fractional part and an exponent part. They can be represented either in decimal form or exponential form. When the floating point literals or decimal form are represented, you must include the decimal point, exponent or both and when you are representing exponential form then you must include integer part, fractional part or both. Some of the examples are 2.15343L, 7.03e32f

Boolean Literals

In Boolean literals there are two types true and false and they are the keywords of C++. The value of true represents true and value of false represents false. The value of true cannot be considered as 1 and value of false cannot be considered as 0.

Character Literals

In the program, the character literals are enclosed within the single quotes and is identified as characters so, if the literal is beginning with L that is uppercase only and it is a wide character literal likes L‘x’ and it should be stored in wchar_t type of variable or else it is a narrow character literal like ‘x’ and it can be stored in a simple char variable type. The character literals can include a plain character like ‘x’, escape sequence like ‘\t’ or a universal character like ‘\u02C0’. In this, some of the characters in C++ are preceded by backslash and have special meaning and are used to represent like tab (\t) or newline (\n).

Let us have a look on some of the escape sequence codes:

Escape Sequence Meaning
\a Alert
\b Backspace
\f Form feed
\n Newline
\\ \character
\’ ‘character
\” “character
\? ?character
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\ooo Octal number of one to three digits
\xhh… Hexadecimal number of one or more digits

Let us have a look at the example:

String Literals

The string literals are enclosed within double quotes as it contains characters that are similar to character literals like plain characters, escape sequences and universal characters. With the help of string literals the long line can be break into multiple lines and separate them by using whitespaces.

Let us have a look at some of the examples:

How to define constants?

The constants can be defined in two ways. They are:

  • By using const keyword.
  • By using #define pre-processor.

Const keyword

The const keyword can use as a prefix to declare the constants.

Let us have a look at the syntax:

const type variable = value;

Let us have a look at the example:

#define pre-processor

The #define pre-processor can be used to define constant in this way:

#define identifier value

Let us have a look at the example:


Like it? Please Spread the word!