Constants

1.4 Constants

Every constant is either an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants, which are described in Section 1.10.1. Every constant has a type that is determined by its value and its notation.

1.4.1 Integer Constants

Integer constants can be represented as ordinary decimal numbers, octal numbers, or hexadecimal numbers:

         A decimal constant (base 10) begins with a digit that is not 0; for example: 1024

         An octal constant (base 8) begins with a 0; for example: 012

         A hexadecimal constant (base 16) begins with the two characters 0x or 0X; for example: 0x7f, 0X7f, 0x7F, 0X7F. The hexadecimal digits A to F are not case-sensitive.

The type of an integer constant, if not explicitly specified, is the first type in the appropriate hierarchy that can represent its value.

For decimal constants, the hierarchy of types is:

int, long, unsigned long, long long(*).

For octal or hexadecimal constants, the hierarchy of types is:

int, unsigned int, long, unsigned long, long long(*), 
unsigned long long(*).

Thus, integer constants normally have type int. The type can also be explicitly specified by one of the suffixes L or l (for long), LL(*) or ll(*) (for long long(*)), and/or U or u (for unsigned). Table 1-6 provides some examples.

Table 1-6. Examples of integer constants

Decimal

Octal

Hexadecimal

Type

15
017
0xf
int
32767
077777
0x7FFF
int
10U
012U
0xAU
unsigned int
32768U
0100000U
0x8000u
unsigned int
16L
020L
0x10L
long
27UL
033ul
0x1BUL
unsigned long

The macros in Table 1-7 are defined to represent constants of an integer type with a given maximum or minimum width N (e. g., = 8, 16, 32, 64). Each of these macros takes a constant integer as its argument and is replaced by the same value with the appropriate type.

Table 1-7. Macros for integer constants of minimum or maximum width

Macro

Return type

INTMAX_C()
intmax_t
UINTMAX_C()
uintmax_t
INTN_C()
int_leastN_t
UINTN_C()
uint_leastN_t

1.4.2 Floating Constants

A floating constant is represented as a sequence of decimal digits with one decimal point, or an exponent notation. Some examples are:

41.9 
5.67E-3  // The number  5.67*10-3

E can also be written as e. The letter P or p is used to represent a floating constant with an exponent to base 2 (ANSI C99); for example:

2.7P+6  // The number  2.7*26

The decimal point or the notation of an exponent using E, e, P(*), or p(*) is necessary to distinguish a floating constant from an integer constant.

Unless otherwise specified, a floating constant has type double. The suffix F or f assigns the constant the type float; the suffix L or l assigns it the type long double. Thus the constants in the previous examples have type double, 12.34F has type float, and 12.34L has type long double.

Each of the following constants has type double. All the constants in each row represent the same value:

5.19
0.519E1
0.0519e+2
519E-2
12.
12.0
.12E2
12e0
370000.0
37e+4
3.7E+5
0.37e6
0.000004
4E-6
0.4e-5
.4E-5

1.4.3 Character Constants and String Literals

A character constant consists of one or more characters enclosed in single quotes. Some examples are:

'0'  'A'  'ab'

Character constants have type int. The value of a character constant that contains one character is the numerical value of the representation of the character. For example, in the ASCII code, the character constant '0' has the value 48, and the constant 'A' has the value 65.

The value of a character constant that contains more than one character is dependent on the given implementation. To ensure portability, character constants with more than one character should be avoided.

Escape sequences such as '\n' may be used in character constants. The characters ' and \ can also be represented this way.

The prefix L can be used to give a character constant the type wchar_t; for example:

L'A'  L'\x123'

A string literal consists of a sequence of characters and escape sequences enclosed in double quotation marks; for example:

"I am a string!\n"

A string literal is stored internally as an array of char (see Section 1.10) with the string terminator '\0'. It is therefore one byte longer than the specified character sequence. The empty string occupies exactly one byte. A string literal is also called a string constant, although the memory it occupies may be modified.

The string literal "Hello!", for example, is stored as a char array, as shown in Figure 1-3.

Figure 1-3. A string literal stored as a char array

figs/cpr_03.gif

String literals that are separated only by whitespace are concatenated into one string. For example:

"hello" " world!" is equivalent to "hello world!".

Because the newline character is also a whitespace character, this concatenation provides a simple way to continue a long string literal in the next line of the source code.

Wide string literals can also be defined as arrays whose elements have type wchar_t. Again, this is done by using the prefix L; for example:

L"I am a string of wide characters!"

 



C Pocket Reference
C Pocket Reference
ISBN: 0596004362
EAN: 2147483647
Year: 2002
Pages: 29

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net