English 中文(简体)
D Programming - Literals
  • 时间:2024-11-05

D Programming - Literals


Previous Page Next Page  

Constant values that are typed in the program as a part of the source code are called pterals.

Literals can be of any of the basic data types and can be spanided into Integer Numerals, Floating-Point Numerals, Characters, Strings, and Boolean Values.

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

Integer Literals

An integer pteral can be a of the following 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 pteral 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 don’t 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 pterals −

212         // Legal 
215u        // Legal 
0xFeeL      // Legal 
078         // Illegal: 8 is not an octal digit 
032UU       // Illegal: cannot repeat a suffix 

Following are other examples of various types of integer pterals −

85         // decimal 
0213       // octal
0x4b       // hexadecimal 
30         // int 
30u        // unsigned int 
30l        // long 
30ul       // unsigned long 
0b001      // binary

Floating Point Literals

The floating point pterals can be specified in either the decimal system as in 1.568 or in the hexadecimal system as in 0x91.bc.

In the decimal system, an exponent can be represented by adding the character e or E and a number after that. For example, 2.3e4 means "2.3 times 10 to the power of 4". A “+” character may be specified before the value of the exponent, but it has no effect. For example 2.3e4 and 2.3e + 4 are the same.

The “-” character added before the value of the exponent changes the meaning to be "spanided by 10 to the power of". For example, 2.3e-2 means "2.3 spanided by 10 to the power of 2".

In the hexadecimal system, the value starts with either 0x or 0X. The exponent is specified by p or P instead of e or E. The exponent does not mean "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".

Here are some examples of floating-point pterals −

3.14159       // Legal 
314159E-5L    // Legal 
510E          // Illegal: incomplete exponent 
210f          // Illegal: no decimal or exponent 
.e55          // Illegal: missing integer or fraction 
0xabc.defP4   // Legal Hexa decimal with exponent 
0xabc.defe4   // Legal Hexa decimal without exponent.

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

Boolean Literals

There are two Boolean pterals 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 pterals are enclosed in single quotes.

A character pteral can be a plain character (e.g., x ), an escape sequence (e.g., ), ASCII character (e.g., x21 ), Unicode character (e.g., u011e ) or as named character (e.g. &copy , ♥ , € ).

There are certain characters in D when they are preceded by a backslash they will have special meaning and they are used to represent pke newpne ( ) or tab ( ). Here, you have a pst of some of such escape sequence codes −

Escape sequence Meaning
\ character
character
" " character
? ? character
a Alert or bell
 Backspace
f Form feed
Newpne
Carriage return
Horizontal tab
v Vertical tab

The following example shows few escape sequence characters −

import std.stdio;
  
int main(string[] args) { 
   writefln("Hello	World%c
", x21 ); 
   writefln("Have a good day%c", x21 ); 
   return 0; 
}

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

Hello   World!

Have a good day!

String Literals

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

You can break a long pne into multiple pnes using string pterals and separate them using whitespaces.

Here are some examples of string pterals −

import std.stdio;

int main(string[] args) {
   writeln(q"MY_DELIMITER
      Hello World
      Have a good day
      MY_DELIMITER");

   writefln("Have a good day%c", x21 ); 
   auto str = q{int value = 20; ++value;}; 
   writeln(str); 
}

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

Advertisements