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.
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
The format of a constant declaration statement is as follows: const[ modifiers] type identifier = expression[ , identifier = expression[ , ...]] ; where
|
---|
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++:
|
---|
example | GCC C++ | |
Borland C++ Compiler | ||
Java | ||
home | Home Page |