A constant variable, or constant for short, is a variable that cannot be assigned to.


There are two kinds of constants: symbolic constants and literal constants.

symbolic constant
A symbolic constant is a variable whose value cannot be change once it is initially bound to a value. Its value can be specified only once, but can be referenced multiple times.
literal constant
A literal constant, or literal, is a literal value inserted into source code. It is a constant because its value cannot be changed.
C++

In C++ a symbolic constant can be declared by using either the #define preprocessor directive or a constant declaration statement. A constant must be assigned a value when declared, and then that value cannot be changed.

The format of a #define preprocessor directive is as follows:

#define identifier expression

where

#define is the #define keyword and indicates that a constant is being declared using a preprocessor directive.
identifier is the identifier by which it will be possible to refer to the constant.
expression is the expression that is evaluated and assigned to the constant.

The format of a constant declaration statement is as follows:

const[ modifiers] type identifier = expression[ , identifier = expression[ , ...]] ;

where

const is the const keyword and indicates that a constant is being declared. If the const modifier is omitted, a variable is declared.
modifiers modify the range and precision of the constant and whether it can contain negative numbers.
type is the type specifier of the constant.
identifier is the identifier by which it will be possible to refer to the constant.
= is the assignment operator = and indicates that the value of an expression is to be assigned.
expression is the expression that is evaluated.
, delimits the constants being identified.
; closes the declaration and delimits it from the next statement.

Literal

A literal constant, or literal for short, is an expression within source code that represents a constant value. It is a notation for representing a fixed value in source code.

For example, in C++ or Java: expression 123 is an integer literal that evaluates to a constant value of 123; expression "Hello World" is a string literal that evaluates to a constant value of "Hello World".

Literals are often used to initialise variables, for example:

a = 123
s = "Hello World"

Technically, a literal is assigned a value at compile time, while a variable or constant is assigned at runtime.

C++

The following literals are available in C++:

integer

An integer literal represents an integer and is inserted into source code as a number without a decimal point or exponent, for example 123.

The following suffixes can be appended to an integer literal to specify its primitive data type .

u or U The literal represents an unsigned integer, i.e. it cannot contain a negative value.
l or L The literal represents a long integer.

If an integer literal begins with a nonzero digit, it is inserted into source code as decimal (base-10). The following prefixes can be prepended to an integer literal to change the numeral system to octal (base-8) or hexadecimal (base-16).

0 The numeral system is octal (base-8).
0x or 0X The numeral system is hexadecimal (base-16).
character

A character literal represents a single character and is inserted into source code as one or more characters enclosed in single quotes '', for example 'a'.

The L prefix can be prepended to a character literal to specify that it is a wide character literal. A character literal without the L prefix is an ordinary character literal, also referred to as a narrow character literal.

A narrow character literal inserted into code as one character between single quotes has type char and represents the numeric value of the character encoded using a single-byte character set (SBCS). A narrow character literal inserted into code as more than one character between single quotes is a multicharacter literal. A multicharacter literal has type int and represents the numeric value of the character encoded using a multi-byte character set (MBCS).

A wide character literal has type wchar_t and represents the numeric value of the character encoded using a wide character set (e.g. Unicode).

A character literal can contain any of the following:

  • any member of the source character set except a single quote ', backslash \ or new line character
  • an escape sequence
  • a universal character name

A universal character name is translated to the encoding, in the execution character set, of the character named. If there is no such encoding, the universal character name is translated to an implementation-defined encoding. A universal character name can be introduced when an extended character is encountered in the source code text. Therefore, all extended characters are described in terms of universal character names.

floating-point

A floating-point literal represents a floating-point number and is inserted into source code as a number with a floating point, for example 1.23, and/or with an exponent, for example 123e45.

A floating-point literal is constructed as follows:

<fractional constant><exponent><suffix>

where

fractional constant
is a sequence of decimal digits that may contain one decimal point . character. The fractional constant consists of an integer part and an optional decimal point and fraction part.
exponent
is a sequence of digit characters preceded by e or E.
suffix
is no character or one of: f; F; l; L

The value of a floating-point literal is <fractional constant> * 10<exponent>

If a suffix is used, it specifies its primitive data type as follows:

f or F The literal represents a single-precision floating point number.
l or L The literal represents a long double-precision floating point number.
string

A string literal represents a string of characters and is inserted into source code as a sequence of characters enclosed in double quotes "", for example "abc".

The L prefix can be prepended to a string literal to specify that it is a wide string literal. A string literal without the L prefix is an ordinary string literal, also referred to as a narrow string literal. A narrow string literal has type char; a wide string literal has type wchar_t.

A string literal can contain any of the following:

  • any member of the source character set except a double quote ", backslash \ or new line character
  • an escape sequence
  • a universal character name

boolean

A boolean literal represents a boolean value and is inserted into source code either as true or false.

Boolean literals are the keywords true and false, and have type bool.


example GCC C++
Borland C++ Compiler
Java
home Home Page