<<

PROGRAMMING POINTERS

Dan Saks

Numeric Literals

The C language provides several Forms of integer literals int. 0x3FL has type (signed) long int. different kinds of constants: integer An integer literal suffix may combine constants such as 10 and 0x1C, floating An integer literal takes different U with either L or LL (in either upper constants such as 1.0 and 6.022e+23, forms: or lower case and in either order), so and character constants such as 'a' and that both 63ul and 0x3FLU have type '\x10'. C also provides string literals • A decimal integer literal (base 10) unsigned long int. such as "ouch!" and "\n". C++ pro- is a non-zero digit followed by zero An integer literal’s suffix influences vides the same kinds of constants and or more decimal digits the literal’s type, but does not deter- literals, except the C++ standard just • A integer literal mine it. The type also depends on the calls all of them literals instead. I do, (base sixteen) is 0x or 0X fol- literal’s value. too. lowed by a sequence of one or Every literal in C and C++ has a type. For example, 10 is obviously an integer. But is it an int (which is Every literal has a type. It may not be obvious, signed by default) an unsigned int, or a short or long variation and it may vary across platforms, but the thereof? Do you even care? If you’re a C programmer, you standard does specify it precisely. might not care. Part of me hopes you’ care, but I’m not absolutely sure you need to. Many C program- more hexadecimal digits Types of integer literals mers apparently get by quite well • An integer literal (base eight) without knowing the exact types of is the digit 0followed by a sequence In general, an integer literal’s type is constants. of zero or more octal digits the smallest integer type no smaller If you’re a C++ programmer, you than int that can hold the literal’s really should care. C++ supports For example, the decimal integer liter- value and still satisfy the constraints function name overloading, and the al 63 can also be expressed as the hexa- imposed by the form and suffix. For exact type of a literal just might decimal integer literal 0x3F or as the example, the type of a decimal integer determine which function is the best octal integer literal 077. literal with no suffix is the smallest match for a particular call. For Any integer literal may have a suffix (the first) of the following types that example, given: that influences its type: can represent its value: int, long int, and in C99, long long i n t . Because int f(int); • U or u specifies that the literal has the maximum values of the types vary unsigned int f(unsigned int); an unsigned type across platforms, the exact types for long int f(long int); • Lor lspecifies that the literal has a many integer literals also vary across long type platforms. which of these functions does f(10) • LL or ll specifies that the literal has Both the C and C++ standards allow call? The answer is: a long long type. (This is available considerable slack in the range of val- in the most recent edition of ues for integer types. The maximum int f(int); Standard C, C99, but not in earlier value for an int must be at least versions of C or in C++) 215–1, and the maximum value for a because the precise type of 10 is long int must be at least 231–1. In (signed) int. For example, 63u has type unsigned practice, this means that an int must

Embedded Systems Programming SEPTEMBER 2000 113 PROGRAMMING POINTERS

In C++, the only form of a floating literal is a decimal floating literal, in which Appending the letter L or l to a floating all of the digits (in both the significant part and the exponent part) are deci- literal makes its type long double. For mal digits. C99 also provides hexadecimal floating literals, in which the digits example, 1.0F has type float, and of the significant part are hexadecimal digits. 6.022e+23L has type long double. Combining F and L (in either case and in either order) in a floating literal is a syntax error. TABLE 1 The type of integer literals in C99 and C++ In C++, the only form of a floating lit- eral is a decimal floating literal, in which Suffix Decimal Literal Hexadecimal Literal all of the digits (in both the significant None int int unsigned int part and the exponent part) are decimal long int long int digits. C99 also provides hexadecimal unsigned long int long long int long long int floating literals, in which the digits of the unsigned long long int significant part are hexadecimal digits. U or u unsigned int unsigned int In a decimal floating literal, the unsigned long int unsigned long int unsigned long long int unsigned long long int exponent part is optional. If present, it L or l long int long int begins with E or e followed by decimal unsigned long int long long int long long int digits representing a power of 10. In a unsigned long long int hexadecimal floating literal, the expo- LL or ll long long int long long int E e unsigned long long int nent part is mandatory. Since and Both U and L (in either unsigned long int unsigned long int are valid hexadecimal digits, a hexadec- case and either order) unsigned long long int unsigned long long int imal floating literal uses P or p to mark Both U and LL (in either unsigned long long int unsigned long long int case and either order) the beginning of the exponent part, fol- lowing by decimal digits representing a Lighter areas apply only to C99 power of two. For example, 0x1.FFFFFEp127f is a floating constant occupy at least 16 bits and a long 231-1 has type long int on a 16-bit plat- whose value is 0x1.FFFFFE multiplied by int must occupy at least 32 bits. form, but just plain int on a 32-bit plat- 2127 (decimal). The f at the end of the On any platform, int and long int form. literal specifies that the literal’s type is may occupy more than their minimum For example, given the overloaded float. storage requirement, as long as a long functions: Neither the form nor value of a int occupies at least as much storage as floating literal affects its type. The suf- an int. An unsigned integer type must int f(int); fix letter (or absence thereof) com- occupy the same amount of storage as its unsigned int f(unsigned int); pletely determines the type. corresponding signed integer type. For long int f(long int); instance, a 16-bit platform might use 16 unsigned long int Character and string literals bits for int and unsigned int and 32 f(unsigned long int); bits for long int and unsigned long Integer and floating literals are essential- int. A 32-bit platform might use 32 bits calling f (32768) calls f(long int) ly the same in both C and C++. The dif- for all four types, int, unsigned int, when compiled using C++ for a 16-bit ferences lie in C’s added support for long int, and unsigned long int. platform, but it calls f(int) when com- long long integer types and hexadec- Table 1 describes the rules for deter- piled for a 32-bit platform. On the other imal floating literals. In contrast, charac- mining the type of an integer literal in hand, if you write the call as f(0x8000) ter and string literals behave slightly dif- C99 and C++. A literal whose value can’t (0x8000 is 32,768 as a hexadecimal liter- ferently between C and C++. Those dif- be represented in any of the listed types al), it calls f(unsigned int) on every ferences are my topic for next time. esp produces a compile error. platform. The rules in Table 1 lead to some Dan Saks is the president of Saks & potential portability problems. A deci- Floating literals Associates, a C/C++ training and consulting mal integer literal whose value is not company. He is also a consulting editor for the greater than 32,767 (= 215–1) has type Floating literals offer a few little surpris- C/C++ Users Journal. He served for many int on every standard C and C++ envi- es, too. The type of a floating literal such years as secretary of the C++ standards com- ronment. No surprise there. However, as 1.0 or 6.022e+23 is double, not mittee. With Thomas Plum, he wrote C++ any decimal integer literal whose value is float. Appending the letter F or f to Programming Guidelines (Plum Hall). greater than 215-1 but not greater than a floating literal makes its type float. You can write to him at [email protected].

114 SEPTEMBER 2000 Embedded Systems Programming