Integer constants can be written in decimal (12345
), octal (012345
), or hexadecimal (0x12345
). 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 following suffixes to any integer constant to explicitly specify its D type:
|
|
|
|
|
|
|
|
|
|
Floating-point constants are always written in decimal and must contain either a decimal point (12.345
) or an exponent (123e45
) or both (123.34e-5
). Floating-point constants are assigned the type double
by default. You can apply one of the following suffixes to any floating-point constant to explicitly specify its D type:
|
|
|
|
Character constants are written as a single character or escape sequence enclosed in a pair of single quotes ('a'
). Character constants are assigned the type int
and are equivalent to an integer constant whose value is determined by that character's value in the ASCII character set. You can refer to
ascii
(
5
)
for a list of characters and their values. You can also use any of the special escape sequences shown in the following table in your character constants. D supports the same escape sequences found in ANSI-C.
Table 2.5. D Character Escape Sequences
|
alert |
|
backslash |
|
backspace |
|
question mark |
|
formfeed |
|
single quote |
|
newline |
|
double quote |
|
carriage return |
|
octal value 0 |
|
horizontal tab |
|
hexadecimal value 0x |
|
vertical tab |
|
null character |
You can include more than one character specifier inside single quotes to create integers whose individual bytes 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 endian-ness 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 may contain any of the special character escape sequences shown for character constants above. 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 that 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, as described in Chapter 6, Strings.