Constants—values that don't change during the life of the program—can be numbers, characters, or strings. Your program can also define “symbolic constants,” which are names that represent constant values. This section describes how to specify C constants.
A numeric constant can have any basic data type, and can be specified in decimal, hexadecimal, or octal notation. Table 4.2 shows how to specify numeric constants.
Table 4.2 Constant Specifications
Constant | Type |
255 | decimal int |
0xFF | hexadecimal int (255) |
0377 | octal int (255) |
255L | long int |
255U | unsigned int |
0xFFul | long unsigned hexadecimal int (255) |
15.75E2 | floating point (1575) |
–.123 | floating point |
.123 | floating point |
3e0f | floating point |
A number without a suffix, such as 255, is treated as decimal. The 0x prefix specifies a hexadecimal number, and the 0 (zero) prefix specifies octal (base 8).
If a number doesn't have a decimal point, it is an integer. Integers are signed by default; you can use the suffix U or u to specify an unsigned constant. To specify a long integer, place the suffix L or l after the number.
A floating-point constant contains either a decimal point or an exponent preceded by e or E. It can optionally include the suffix F or f to denote the float type or the suffix L or l to denote the long double type.
The C language uses different notation for character and string constants. A single character enclosed in single quotes is a character constant:
'a'
A string constant is 0 or more characters enclosed in double quotes:
"Hello"
A string also ends with a null character (\0), as we'll see in the section “Strings.”
The difference between character and string constants is important when you perform comparisons. The character constant 'a' contains 1 character, but the string constant "a" contains 2 characters: the letter a plus a null character. Because the two values have a different number of characters, any comparison of them is invalid. (See “String Problems” in Chapter 10, “Programming Pitfalls.”)
You can specify special characters, such as the tab and backspace, with a multi-character sequence that begins with a backslash ( \ ). These sequences are sometimes called “escape sequences.” Table 4.3 shows the special character sequences.
Table 4.3 Special Characters
Sequence | Character |
\a [DOS only] | Alert (bell) |
\b | Backspace |
\f [DOS only] | Formfeed |
\n | Newline |
\r | Carriage return |
\t [DOS only] | Horizontal tab |
\v [DOS only] | Vertical tab |
\' | Single quote |
\” | Double quote |
\\ | Backslash |
\ooo | Octal notation |
\xhh | Hexadecimal notation |
\0 | Null |
Some unusual characters don't have a predefined sequence. You can specify these with a backslash (\ ) followed by the hexadecimal or octal number repre-senting the character's ASCII value. For instance, a telecommunications program
might need to specify ASCII 21, the NAK (“not acknowledged”) character. You can specify this character in either hexadecimal notation, '\x15', or octal notation,'\25'. Note that the hexadecimal number begins with \x, while the octal number starts with a backslash alone.
A “symbolic constant” is a user-defined name that represents a constant. Symbolic constants are usually typed in uppercase. For instance, the directive
#define PI 3.14
declares a symbolic constant named PI. Wherever PI occurs in the program, the compiler substitutes 3.14. For more information on symbolic constants and the #define directive, see Chapter 7, “Preprocessor Directives.”