# 1.8 Constants

There are four categories of constants in C:

• Integer constants (such as `63` , `0` , and `42L` )

• Floating-point constants (such as `1.2` , `0.00` , and `77E+2` )

• Character constants (such as `'A'` , `'0'` , and `L'\n'` )

• Enumeration constants (such as ```enum boolean { NO, YES };``` ), where `NO` and `YES` are the enumeration constants

The following sections describe these constants.

The value of any constant must be within the range of representable values for the specified type. Regardless of its type, a constant is a literal or symbolic value that does not change. A constant is also an rvalue, as defined in Section 2.13.

## 1.8.1 Integer Constants

Integer constants are used to represent whole numbers. An integer constant can be specified in decimal, octal, or hexadecimal radix, and can optionally include a prefix that specifies its radix and a suffix that specifies its type. An integer constant cannot include a period or an exponent part.

Follow these rules when specifying an integer constant:

• To specify a decimal integer constant, use a sequence of decimal digits in which the first digit is not 0. The value of a decimal constant is computed in base 10.

• To specify an octal integer constant, start the sequence with a zero (0) and follow the 0 (if necessary) with a sequence composed of the digits 0 to 7. A leading 0 alone signifies the octal number 0. The value of an octal constant is computed in base 8.

• To specify a hexadecimal integer constant, start the hexadecimal sequence with a 0 followed by the character `X` (or `x` ). Follow the `X` or `x` with one or more hexadecimal characters (the digits 0 to 9 and the upper or lowercase letters A to F). The value of a hexadecimal constant is computed in base 16 (the letters A to F have the values 10 to 15, respectively).

Without explicit specification, the type of an integer constant defaults to the smallest possible type that can hold the constant's value, unless the value is suffixed with an `L` , `l` , `U` , or `u` . The following list describes the type assignment of integer constants:

• If the constant has no suffix, and is given in decimal radix, it will have the first type from this list capable of storing the value: `int` , `long int` , `unsigned long int` .

• If the constant has no suffix, and is given in octal or hexadecimal radix, it will have the first type from this list capable of storing the value: `int` , ```unsigned int``` , `long int` , ```unsigned long int``` .

• If the constant has the `U` or `u` suffix, it will have the first type from this list capable of storing the value: `unsigned int` , ```unsigned long int``` .

• If the constant has the `L` or `l` suffix, it will have the first type from this list capable of storing the value: `long int` , ```unsigned long int``` .

• If the constant has both `U` and `L` suffixes (or the lowercase combination), it will have type `unsigned long int` .

For example, the constant `59` is assigned the `int` data type by default, but the constant `59L` is assigned the `long` data type. `59UL` is typed as `unsigned long int` .

Integer constant values are always nonnegative; a preceding minus sign is interpreted as a unary operator, not as part of the constant. If the value exceeds the largest representable integer value (causing an overflow), the compiler issues a warning message and uses the greatest representable value for the integer type. Unsuffixed integer constants can have different types, because without explicit specification the constant is represented in the smallest possible integer type.

## 1.8.2 Floating-Point Constants

A floating-point constant has a fractional or exponential part. Floating-point constants are always interpreted in decimal radix (base 10). An optional suffix can be appended to show the constant's type. Floating-point constants can be expressed with decimal point notation, signed exponent notation, or both. A decimal point without a preceding or following digit is not allowed (for example, `.E1` is illegal). Table 1-5 shows examples of valid notational options.

The significand part of the floating-point constant (the whole number part, the decimal point, and the fractional part) may be followed by an exponent part, such as `32.45E2` . The exponent part (in the previous example, `E2` ) indicates the power of 10 by which the significand part is to be scaled. The precise value after scaling is dependent on your platform. The determining algorithm is described in your platform-specific DEC C documentation.

The default type of a floating-point constant is `double` , unless:

• The value exceeds the largest value representable by type `double` , in which case a compiler overflow warning results. (The result is truncated within the `double` type.)

• An `L` or `l` is appended to the value, which specifies the `long double` type for the floating-point constant.

• An `F` or `f` is appended to the value, which specifies the `float` type for the floating-point constant.

Floating-point constant values must be nonnegative; a preceding minus sign is interpreted as a unary operator, not as part of the constant.

### Table 1-5 Floating-Point Notation

Notation  Value  Type
.0  0.000000  `double`
0.  0.000000  `double`
2.  2.000000  `double`
2.5  2.500000  `double`
2e1  20.00000  `double`
2E1  20.00000  `double`
2.E+1  20.00000  `double`
2e+1  20.00000  `double`
2e-1  0.200000  `double`
2.5e4  25000.00  `double`
2.5E+4  25000.00  `double`
2.5F  2.500000  `float`
2.5L  2.500000  `long double`

## 1.8.3 Character Constants

A character constant is any character from the source character set enclosed in apostrophes. Character constants are represented by objects of type `int` . For example:

```char alpha = 'A';
```

Characters such as the new-line character, single quotation marks, double quotation marks, and backslash can be included in a character constant by using escape sequences as described in Section 1.8.3.3. All valid characters can also be included in a constant by using numeric escape sequences, as described in Section 1.8.3.4.

The value of a character constant containing a single character is the numeric value of the character in the current character set. Character constants containing multiple characters within the single quotation marks have a value determined by the compiler. The value of a character constant represented by an octal or hexadecimal escape sequence is the same as the octal or hexadecimal value of the escape sequence. The value of a wide character constant (discussed in Section 1.8.3.1) is determined by the `mbtowc` library function.

There is a limit of four characters for any one character constant. Enclosing more than four characters in single quotation marks (such as `'ABCDE'` ), generates an overflow warning.

Note that the byte ordering of character constants is platform specific.

### 1.8.3.1 Wide Characters

C provides for an extended character set through the use of wide characters. Wide characters are characters too large to fit in the `char` type. The `wchar_t` type is typically used to represent a character constant in a character set requiring more than 256 possible characters, because 8 bits can represent only 256 different values.

A character constant in the extended character set is written using a preceding `L` , and is called a wide-character constant. Wide-character constants have an integer type, `wchar_t` , defined in the `<stddef.h>` header file. Wide-character constants can be represented with octal or hexadecimal character escape sequences, just like normal character escape sequences, but with the preceding `L` .

Strings composed of wide characters can also be formed. The compiler allocates storage as if the string were an array of type `wchar_t` , and appends a wide null character (\0) to the end of the string. The array is just long enough to hold the characters in the string and the wide null character, and is initialized with the specified characters.

The following examples show valid wide-character constants and string literals:

```wchar_t wc = L'A';
wchar_t wmc = L'ABCD';
wchar_t *wstring = L"Hello!";
wchar_t *x = L"Wide";
wchar_t z[] = L"wide string";
```

DEC C stores `wchar_t` objects as `unsigned long` objects (OpenVMS) or `unsigned int` objects (Digital UNIX) in 32 bits of storage. The null character at the end of a wide-character string is 32 bits long.

### 1.8.3.2 Multibyte Characters

Some programmers requiring an extended character set have used shift-dependent encoding schemes to represent the non-ASCII characters in the normal `char` size of 8 bits. This encoding results in multibyte characters. ANSI C supports these encoding schemes, in addition to providing the wide-character type `wchar_t` .

In accordance with the ANSI standard, DEC C recognizes multibyte characters in the following contexts:

• String literals

• Character constants

For proper input and output of the multibyte character encodings, and to prevent conflicts with existing string processing routines, note the following rules governing the use of multibyte characters:

• A byte with all bits set to zero is always recognized as a null character. Null characters can only be single bytes.

• A null character cannot occur as the second or subsequent byte of a multibyte character.

Transforming multibyte characters to wide-character constants and wide string literals eases the programmer's problems when dealing with shift-state encoding. There are several C library functions available for transforming multibyte characters to wide characters and back. See Chapter 9 for more information.

### 1.8.3.3 Character Escape Sequences

Characters that cannot be displayed on a standard terminal, or that have special meaning when used in character constants or string literals, can be entered as source characters by entering them as character escape sequences. A backslash (\) begins each character escape sequence. Each of the escape sequences is stored in a single `char` or `wchar_t` object. Table 1-6 lists the ANSI- defined escape sequences.

### Table 1-6 Character Escape Sequences

Character  Escape Sequence
Backspace  \b
Form Feed  \f
New line  \n
Carriage Return  \r
Horizontal Tab  \t
Vertical Tab  \v
Backslash  \\
Single Quote  \'
Double Quote  \"
Question Mark  \?

No other character escape sequences are valid. If another sequence is encountered in the source code, the compiler issues a warning and the backslash character is ignored.

An example of a character escape sequence use follows:

```printf ("\t\aReady\?\n");
```

Upon execution, this results in an alert bell and the following prompt:

```     Ready?
```

### 1.8.3.4 Numeric Escape Sequences

The compiler treats all characters as an integer representation, so it is possible to represent any character in the source code with its numeric equivalent. This is called a numeric escape sequence. The character is represented by typing a backslash (`\` ), followed by the character's octal or hexadecimal integer equivalent from the current character set (see Appendix C for the ASCII equivalence tables). For example, using the ASCII character set, the character `A` can be represented as `\101` (the octal equivalent) or `\x41` (the hexadecimal equivalent). A preceding `0` in the octal example is not necessary because octal values are the default in numeric escape sequences. A lowercase `x` following the backslash indicates a hexadecimal representation. For example, `\x5A` is equivalent to the character `Z` .

An example of numeric escape sequences follows:

```#define NUL '\0'   /*  Defines logical null character   */

char x[] = {'\110','\145','\154','\154','\157','\41','\0'};
/*  Initializes x with "Hello!"  */
```

The escape sequence extends to three octal digits, or the first character that is not an octal digit, whichever is first. Therefore, the string `"\089"` is interpreted as four characters: `\0` , `8` , `9` , and `\0` .

With hexadecimal escape sequences, there is no limit to the number of characters in the escape sequence, but the result is not defined if the hexadecimal value exceeds the largest value representable by the `unsigned char` type for an normal character constant, or the largest value representable by the `wchar_t` type for a wide-character constant. For example, `'\x777'` is illegal.

In addition, hexadecimal escape sequences with more than three characters provoke a warning if the error-checking compiler option is used.

String concatenation can be used to specify a hexadecimal digit following a hexadecimal escape sequence. In the following example, `a` is initialized to the same value in both cases:

```char a[] = "\xff" "f";
char a[] = {'\xff', 'f', '\0'};
```

Using numeric escape sequences can result in a nonportable program if the executing machine uses a different character set. Another threat to portability exists if arithmetic operations are performed on the integer character values, because multiple character constants (such as `'ABC'` can be represented differently on different machines.

## 1.8.4 Enumeration Constants

An enumerated type specifies one or more enumeration constants to define allowable values for the enumerated type. Enumeration constants have the type `int` . See Section 3.6 for details on the declaration and use of enumerated types.