D Literals - D Programming Language

What are D Literals?

Constant values that are entered in the program as a part of the source code are called literals.
Literals can be of any of the simple data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Again, literals are treated just like regular variables except that their values cannot be modified after their definition.

Integer literals:

An integer literal can be a of the folowing types:

• Decimal uses the normal number represention with the first digit cannot be 0 as that digit is reserved for indicating the octal system.This does not include 0 on its own: 0 is zero.
• Octal uses 0 as prefix to number.
• Binary uses 0b or 0B as prefix
• Hexadecimal uses 0x or 0X as prefix.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

When you dont use a suffix the compiler itself chooses between int, uint, long and ulong based on the magnitude of the value.
Here are some examples of integer literals:

Following areother examples of various types of Integer literals:

Floating-point literals:

The floating point literals can be laid out in either the decimal device as in 1.568 or in the hexadecimal device as in 0x91.bc.

within the decimal system, an exponent can be represented by way of adding the character e or E and more than a few after that. as an example, 2.3e4 means "2.3 instances 10 to the power of 4". A + individual may be targeted before the cost of the exponent, but it has no impact. for example 2.3e4 and 2.3e + 4 are the same. The - character added before the price of the exponent changes the meaning to be "divided by 10 to the power of". for example, 2.3e-2 means "2.3 divided by 10 to the power of 2".

within the hexadecimal system, the value starts with either 0x or 0X. The exponent is specific by means of p or P instead of e or E. The exponent does not suggest "10 to the power of", but "2 to the power of". for example, the P4 in 0xabc.defP4 means "abc.de times 2 to the power of 4".

By default, the type of a floating point literal is double. The f and F mean float, and the L specifier means real.

Boolean literals:

There are two Boolean literals and they are part of standard D keywords:

• A value of true representing true.
• A value of false representing false.

You should not consider the value of true equal to 1 and value of false equal to 0.

Character literals:

Character literals are enclosed in single quotes.

A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), ASCII character (e.g., '\x21'), Unicode character (e.g., '\u011e') or as named character (e.g. '\©','\♥', '\€' ).

There are certain characters in D when they are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here, you have a list of some of such escape sequence codes:

 Escape sequence Meaning \\ \ character \' ' character \" " character \? ? character \a Alert or bell \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab

Following is the example to show few escape sequence characters:

When the above code is compiled and executed, it produces the following result:

String literals:

String literals are enclosed in double quotes. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

You can break a long line into multiple lines using string literals and separate them using whitespaces.

Here are some examples of string literals.

In the above example, you can find the use of q"MY_DELIMITER MY_DELIMITER" to represent multi line characters. Also, you can see q{} to represent an D language statement itself.