A common area that people don't even know exist in C++, is called literals or literal constants. But even though you may not have heard of the term before, you no doubt have been using them all the time. For example, 2.0f is a float literal, while 2.0 is a double literal. 0x1 is a hex int literal, while 0x1L is a long int literal. This is important to remember, especially when precision and data size is involved. You may be losing out on data, and you didn't even know you were!
Here's a quick table run-down, taken from
http://cpp.comsci.us/etymology/literals.html.
| Type |
Base |
Example |
Description |
| char * |
ASCII |
"hello" |
Any string of characters enclosed in double quotes (") (see
Note 1) |
| char |
ASCII |
'1' |
One character in single quotes (') |
| unsigned short int |
ASCII |
L'ab' |
One or two characters in single quotes ('), preceded by the
letter L |
| int |
octal |
01 |
Any octal number (digits 0-7) beginning with a 0 (zero) |
| decimal |
1 |
Any decimal number (digits 0-9) not beginning with a 0
(zero) |
| hexadecimal |
0x1 |
0X (zero X) or 0x (zero x) followed by any hexadecimal number
(digits 0-F) |
| ASCII |
'ABC' |
Two to four characters in single quotes (') |
| unsigned int |
octal |
01U |
Any octal number (digits 0-7) beginning with a 0 (zero) and
followed by U or u |
| decimal |
1U |
Any decimal number (digits 0-9) not beginning with a 0 (zero)
and followed by U or u |
| hexadecimal |
0x1U |
0X (zero X) or 0x (zero x) followed by any hexadecimal number
(digits 0-F) and followed by U or u |
| long int |
octal |
01L |
Any octal number (digits 0-7) beginning with a 0 (zero) and
followed by L or l |
| decimal |
1L |
Any decimal number (digits 0-9) not beginning with a 0 (zero)
and followed by L or l |
| hexadecimal |
0x1L |
0X (zero X) or 0x (zero x) followed by any hexadecimal number
(digits 0-F) and followed by L or l |
| unsigned long int |
octal |
01UL |
Any octal number (digits 0-7) beginning with a 0 (zero) and
followed by U or u and L or l |
| decimal |
1UL |
Any decimal number (digits 0-9) not beginning with a 0 (zero)
and followed by U or u and L or l |
| hexadecimal |
0x1UL |
0X (zero X) or 0x (zero x) followed by any hexadecimal number
(digits 0-F) and followed by U or u and L or l |
| float |
decimal |
12.3F |
Any number (digits 0-9) containing a decimal point (.) and
followed by F or f |
| decimal |
12E1F |
Any number (digits 0-9) followed by E or e, followed by an
exponent of 10 (12E1 = 12 * 101 = 120.), and followed by F or
f |
| double |
decimal |
12.3 |
Any number (digits 0-9) containing a decimal point (.) |
| decimal |
12E1 |
Any number (digits 0-9) followed by E or e and followed by an
exponent of 10 (12E1 = 12 * 101 = 120.) |
| long double |
decimal |
12.3L |
Any number (digits 0-9) containing a decimal point (.) and
followed by L or l |
| decimal |
12E1L |
Any number (digits 0-9) followed by E or e, followed by an
exponent of 10 (12E1 = 12 * 101 = 120.), and followed by L or
l |
| Notes:
String constants are stored as the literal characters followed
by a byte of binary 0. The value returned is a pointer to the
first character.
|
There is the string class in C++ standard library, but it is a complex type.
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website: