2.8.3 Constants

Integer constants can be written in decimal (12345), octal (012345), or hexadecimal (0x12345) format. Octal (base 8) constants must be prefixed with a leading zero. Hexadecimal (base 16) constants must be prefixed with either 0x or 0X. Integer constants are assigned the smallest type among int, long, and long long that can represent their value. If the value is negative, the signed version of the type is used. If the value is positive and too large to fit in the signed type representation, the unsigned type representation is used. You can apply one of the suffixes listed in the following table to any integer constant to explicitly specify its D type.

Suffix

D type

u or U

unsigned version of the type selected by the compiler

l or L

long

ul or UL

unsigned long

ll or LL

long long

ull or ULL

unsigned long long

Floating-point constants are always written in decimal format and must contain either a decimal point (12.345), an exponent (123e45), or both ( 123.34e-5). Floating-point constants are assigned the type double by default. You can apply one of the suffixes listed in the following table to any floating-point constant to explicitly specify its D type.

Suffix

D type

f or F

float

l or L

long double

Character constants are written as a single character or escape sequence that is enclosed in a pair of single quotes ('a'). Character constants are assigned the int type rather than char and are equivalent to an integer constant with a value that is determined by that character's value in the ASCII character set. See the ascii(7) manual page for a list of characters and their values. You can also use any of the special escape sequences that are listed in the following table in your character constants. D supports the same escape sequences as those found in ANSI C.

Table 2.6 Character Escape Sequences

Escape Sequence

Represents

Escape Sequence

Represents

\a

alert

\\

backslash

\b

backspace

\?

question mark

\f

form feed

\'

single quote

\n

newline

\"

double quote

\r

carriage return

\0oo

octal value 0oo

\t

horizontal tab

\xhh

hexadecimal value 0xhh

\v

vertical tab

\0

null character


You can include more than one character specifier inside single quotes to create integers with individual bytes that are initialized according to the corresponding character specifiers. The bytes are read left-to-right from your character constant and assigned to the resulting integer in the order corresponding to the native endianness of your operating environment. Up to eight character specifiers can be included in a single character constant.

Strings constants of any length can be composed by enclosing them in a pair of double quotes ("hello"). A string constant may not contain a literal newline character. To create strings containing newlines, use the \n escape sequence instead of a literal newline. String constants can contain any of the special character escape sequences that are shown for character constants previously. Similar to ANSI C, strings are represented as arrays of characters terminated by a null character (\0) that is implicitly added to each string constant you declare. String constants are assigned the special D type string. The D compiler provides a set of special features for comparing and tracing character arrays that are declared as strings. See Section 2.11, “DTrace Support for Strings” for more information.