C Constants
A “constant” is a number, character, or character string that can be used as a value in a program. Use constants to represent floating-point, integer, enumeration, or character values that cannot be modified.
Syntax
constant :
floating-point-constant
integer-constant
enumeration-constant
character-constant
Constants are characterized by having a value and a type. Floating-point, integer, and character constants are discussed in the next three sections. Enumeration constants are described in Enumeration Declarations in Chapter 3.
C Character Constants
A “character constant” is formed by enclosing a single character from the representable character set within single quotation marks (' '). Character constants are used to represent characters in the execution character set.
Syntax
character-constant :
'c-char-sequence'
L'c-char-sequence'
c-char-sequence :
c-char
c-char-sequence c-char
c-char :
Any member of the source character set except the single quotation mark ('), backslash (), or newline character
escape-sequence
escape-sequence :
simple-escape-sequence
octal-escape-sequence
hexadecimal-escape-sequence
simple-escape-sequence : one of
\a \b \f \n \r \t \v
' " \ ?
octal-escape-sequence :
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence :
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
C Integer Constants
An “integer constant” is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed.
Syntax
integer-constant :
decimal-constant integer-suffix opt
octal-constant integer-suffix opt
hexadecimal-constant integer-suffix opt
decimal-constant :
nonzero-digit
decimal-constant digit
octal-constant :
0
octal-constant octal-digit
hexadecimal-constant :
0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit
nonzero-digit : one of
1 2 3 4 5 6 7 8 9
octal-digit : one of
0 1 2 3 4 5 6 7
hexadecimal-digit : one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix :
unsigned-suffix long-suffix opt
long-suffix unsigned-suffix opt
unsigned-suffix : one of
u U
long-suffix : one of
l L
64-bit integer-suffix :
i64
Integer constants are positive unless they are preceded by a minus sign (–). The minus sign is interpreted as the unary arithmetic negation operator. (See Unary Arithmetic Operators in Chapter 4 for information about this operator.)
If an integer constant begins with the letters 0x or 0X, it is hexadecimal. If it begins with the digit 0, it is octal. Otherwise, it is assumed to be decimal.
The following lines are equivalent:
0x1C /* = Hexadecimal representation for decimal 28 /
034 / = Octal representation for decimal 28 */
No white-space characters can separate the digits of an integer constant. These examples show valid decimal, octal, and hexadecimal constants.
/* Decimal Constants */
10
132
32179
/* Octal Constants */
012
0204
076663
/* Hexadecimal Constants */
0xa or 0xA
0x84
0x7dB3 or 0X7DB3
C Floating-Point Constants
A “floating-point constant” is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent. Use floating-point constants to represent floating-point values that cannot be changed.
Syntax
floating-point-constant :
fractional-constant exponent-part opt floating-suffix opt
digit-sequence exponent-part floating-suffix opt
fractional-constant :
digit-sequence opt . digit-sequence
digit-sequence .
exponent-part :
e sign opt digit-sequence
E sign opt digit-sequence
sign : one of
digit-sequence :
digit
digit-sequence digit
floating-suffix : one of
f l F L
You can omit either the digits before the decimal point (the integer portion of the value) or the digits after the decimal point (the fractional portion), but not both. You can leave out the decimal point only if you include an exponent. No white-space characters can separate the digits or characters of the constant.
The following examples illustrate some forms of floating-point constants and expressions:
15.75
1.575E1 /* = 15.75 /
1575e-2 / = 15.75 /
-2.5e-3 / = -0.0025 /
25E-4 / = 0.0025 */
Floating-point constants are positive unless they are preceded by a minus sign (–). In this case, the minus sign is treated as a unary arithmetic negation operator. Floating-point constants have type float, double, long, or long double.
A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double. For example:
100L /* Has type long double /
100F / Has type float /
100D / Has type double */
Note that the Microsoft C compiler maps long double to type double. See Storage of Basic Types in Chapter 3 for information about type double, float, and long.
You can omit the integer portion of the floating-point constant, as shown in the following examples. The number .75 can be expressed in many ways, including the following:
.0075e2
0.075e1
.075e1
75e-2