Constants are expressions with fixed value.
Often are used literals as kind of constants but they are alterable. In previous chapters I have used this kind of literals to initialize a value.
Example: |
---|
a = 7; |
Literal constants can be classified into integer, floating-point, characters, strings, Boolean, pointers and user defined literals.
Integer values are values like -64, 0, 113, 5078 and they are not enclosed in quotes or any other special characters.
All literal constants for integer numbers in C can not alone defined as decimal numbers but it is possible to define it as octal or hexadecimal numbers too.
Example: | |
---|---|
1010 | //decimal |
1762 | //octal |
0x3F2 | //hexadecimal |
All of these numbers present the same number 1010. The type by default integer literals are int, but are appended certain suffixes to an integer literal than it's specified to a different integer type.
Suffix | Type modifier |
---|---|
u or U | unsigned |
l or L | long |
ll or LL | long long |
Unsigned may be combined with any of the other two in any order to form unsigned long or unsigned long long.
Example: | |
---|---|
1010 | //int |
1010l | //long |
1010ul | //unsigned long |
1010LLU | //unsigned long long |
The suffix can be specified with upper or lower case letters.
Floating point numerals are real values, of integer or decimal values, with or without their exponents. They can include either a decimal point and an e character for example 4e10 that expresses four times ten high ten or by the Xth value of an integer or a decimal value times ten and the Xth height.
Example: | |
---|---|
3.14159 | //PI 3.14159 |
5.074e25 | //5.074 x 10^25 |
2.5e-15 | //2.5 x 10^-15 |
1.0 | //1.0 |
The default type for floating point literals is double. Floating point type of float or long double can be specified by adding f or F for float and l or L for long double.
Example: | |
---|---|
3.14159L | //long double |
2.5e10f | //float |
Any of the letters e, f, l can be written in lower or upper case letters with no difference in meaning.
Character and strings literals are enclosed in quotes.
Example: |
---|
'a' |
"Hello world!" |
Single characters are enclosed in single quotes (') and strings are enclosed in double quotes (").
Character and string literals can represent special characters handed on C language that forces a newline into output (\n) for example. All this special characters begins with a backslash like \n (for newline) or \t (for tab).
List of escape codes:
Escape Code | Description |
---|---|
\n | newline |
\r | carriage return key |
\t | tab |
\v | vertical tab |
\b | backspace |
\f | form feed (page feed) |
\a | alert (beep) |
\' | single quote (') |
\" | double quote (") |
\? | question mark (?) |
\\ | Backslash (\) |
All circuits are understand two states power on and power off or 0 and 1 and internally of computers are represented characters as numerical binary codes. But binary codes are hard to read and extensions of base-8 (octal) or base-16 (hexadecimal) are more readably and this codes are collected into an ASCII table. To using an ASCII code in C by type in a backslash and followed digits for octal (\65) and for hexadecimal followed after backslash an x character and than hexadecimal digits (\x4a).
It is possible to concatenate several separated string literals to a single string literal in C++.
Example: | |
---|---|
string | my_string = "This forms " "a single " "string"; |
This definition of a string is the same like this.
Example: | |
---|---|
string | my_string = "This forms a single string"; |
A string can wrote into seperate lines with this trick:
my_string = "This is a string in \ |
two lines"; |
All the character literals and string literals descriped above are made of characters of type char. A different character type can be specified by using one of the following prefixes:
Prefix | Character Type |
---|---|
u | char16_t |
U | char32_t |
L | wchar_t |
All prefixes are case sensitive for example lowercase for char16_t and uppercase for char32_t and wchar_t.
For string literals exists prefixes as follows:
Prefix | Description |
---|---|
u8 | The string literal is encoded in the executable using UTF-8 |
R | The string literal is a raw string |
In raw strings, backslashes and single and double quotes are all valid characters. The content of the literal is delimited by an initial R"sequence( and a final )sequence", where sequence is any sequence of characters (including an empty sequence). The content of the string is what lies inside the parenthesis, ignoring the delimiting sequence itself.
Example: |
---|
R"(string with \\backslash) |
R"&%$(string with \\backslash)&%$" |
Both strings are equivalent to "string with \\backslash". The R prefix can be combined with other prefixes, such as u, L or u8 too.
Three keyword literals exist in C++: true, false and nullptr:
Example: | |
---|---|
bool | foo = true; |
bool | bar = false; |
int | *p = nullptr; |
It is convinient to define constants for calculations.
Example: | |
---|---|
const double | pi = 3.1415926; |
const char | newline = '\n'; |
We can then use these names instead of the literals they were defined to:
1 | #include <iostream> |
2 | using namespace std; |
3 | |
4 | const double pi = 3.1415926; |
5 | const char newline = '\n'; |
6 | double circumference(double); //function prototype |
7 | |
8 | int main(int argc, char **argv) { |
9 |     double radius; |
10 |      |
11 |     cout << "Please insert radius: "; |
12 |     cin >> radius; |
13 |      |
14 |     cout << "The circumference is: " << circumference(radius) << newline; |
15 |      |
16 |     return 0; |
17 | } |
18 | |
19 | double circumference(double radius) { |
20 |     return 2 * pi * radius; |
21 | } |
With preprocessor definitions are possible to define name constants too. They have the following form:
#define identifier replacement
Such one defined directive is performed by preprocessor befor the program is compiled and is not checked of validity or syntax. It can be a sequence of characters or a constant like PI.Example:
1 | #include <iostream> |
2 | using namespace std; |
3 | |
4 | #define PI 3.14159 |
5 | #define NEWLINE '\n' |
6 | |
7 | int main(int argc, char **argv) { |
8 |     double r = 5.0; // radius |
9 |     double circle; |
10 | |
11 |     circle = 2 * PI * r; |
12 |     cout << circle; |
13 |     cout << NEWLINE; |
14 | |
15 |     return 0; |
16 | } |