|
|
HP C/HP-UX Reference Manual: Workstations and Servers > Chapter 2 Lexical Elements Constants |
|
A constant is a primary expression whose literal or symbolic value does not change. Each constant has a value and a type. Both attributes are determined from its form. Constants are evaluated at compile time whenever possible. This means that expressions such as
are automatically interpreted as a single constant at compile time. Floating constants represent floating-point values.
A floating constant has a value part that may be followed by an exponent part and a suffix specifying its type. The value part may include a digit sequence representing the whole-number part, followed by a period (.), followed by a digit sequence representing the fraction part. The exponent includes an e or an E followed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part must be used; either the period or the exponent part must be used. Refer to Chapter 10 “HP C/HP-UX Implementation Topics” for the format of floating-point numbers. A floating constant may include a suffix that specifies its type. F or f specifies type float (single precision). L or l specifies long double (quad precision). The default type (unsuffixed) is double. Integer constants represent integer values.
An integer constant begins with a digit, but has no period or exponent part. It may have a prefix that specifies its base (decimal, octal, or hexadecimal) and suffix that specifies its type. Refer to Chapter 10 “HP C/HP-UX Implementation Topics” for information on the size and type of integer constants. Octal constants begin with a zero and can contain only octal digits. Several examples of octal constants are:
Hexadecimal constants begin with either 0x or 0X. The case of the x character makes no difference to the constant's value. The following are examples of hexadecimal constants:
The suffix L or l stands for long. The suffix ll, LL, lL, or Ll stands for long long. The suffix U or u stands for unsigned. These suffixes can be used on all three types of integer constants (decimal, octal, and hexadecimal). The type of an integer constant is the first of the corresponding list in which its value can be represented, as shown:
0xFFFFu unsigned hexadecimal integer A character constant is a constant that is enclosed in single quotes.
There are two types of character constants — integral character constants and wide character constants. Integral character constants are of type int. They do not have type char. However, because a char is normally converted to an int in an expression, this seldom is a problem. The contents can be ASCII characters, octal escape sequences, or hexadecimal escape sequences. Octal escape sequences consist of a backslash, ( \ ) followed by up to three octal digits. Hexadecimal escape sequences also start with a backslash, which is followed by lowercase x and any number of hexadecimal digits. It is terminated by any non-hexadecimal characters. The digits of the escape sequences are converted into a single 8-bit character and stored in the character constant at that point. For example, the following character constants have the same value:
They all represent the decimal value 65. Character constants are not restricted to one character; multi-character constants are allowed. The value of an integral character constant containing more than one character is computed by concatenating the 8-bit ASCII code values of the characters, with the leftmost character being the most significant. For example, the character constant 'AB' has the value 256*'A'+'B' = 256*65+66 = 16706. Only the rightmost four characters participate in the computation. Wide character constants (type wchar_t) are of type unsigned int. A wide character constant is a sequence of one or more multibyte characters enclosed in single quotes and prefixed by the letter L. The value of a wide character constant containing a single multibyte character is a member of the extended execution character set whose value corresponds to that of the multibyte character. The value of a multibyte character can be found by calling the function mbtowc. For multi-character wide character constants, the entire content of the constant is extracted into an unsigned integer and the resulting character is represented by the final value. Some characters are given special representation in escape sequences. These are nonprinting and special characters that programmers often need to use (listed in Table 2-1 “Special Characters ”). Table 2-1 Special Characters
|
|