# Literals

A literal is a constant value that the compiler interprets and converts to its own internal representation. There are literals that are used to represent the five basic data types and their variants: boolean, integer, floating-point, characters, and strings.

### Boolean Literals

The boolean literals are simply true and false. These literals are used to represent the only two available logical values of a bool type.

### Integers Literals

The integer literals have a numerous variants. To start with, there are three different number bases allowed for literals: 8, 10, and 16. These correspond to octal, decimal, and hexadecimal numbers. The default specification for numbers is decimal. To specify an octal or a hexadecimal number, prepend a 0 or 0x, respectively. For example, the value twenty-eight is specified with a literal as 034, 28, or 0x1c for octal, decimal, or hexadecimal, respectively.

### Floating-Point Literals

The floating point literals have two primary variants. We can use an ordinary decimal representation like this: 201.84. Alternatively, we can use exponents in a manner similar to scientific notation, like this 2.0184e2. The letter e is used to signal a base 10 exponent. We can can use either an uppercase or lowercase e to signal the exponent.

### Character Literals

Single character literals have two variants: one for char and one for wchar_t. The char type stores ASCII characters and wchar_t stores 16 bit Unicode characters. A char literal is written enclosed in single quotes, like this: 'a'. To make the same literal wchar_t, prepend an uppercase L, like this: L'a'. Several escape sequences exist for generating special characters, like ' ' for a new line character. Also, we can use universal character representations, like this '\ub2f4', for representing wide characters.

### String Literals

String literals are similar to single characters in that they come in two variants: char and wchar_t. To distinguish them from single characters, we use double quotes like this: "XoaX.net" or L"Xoax.net" for char and wchar_t strings, respectively. When a string literal is converted, a null character of either type is appended to the string.

### Array Literals

In addition to the literals for fundamental types, we have literals in C++ for arrays. Arrays of chars can be initialized with string literals, just as we do with pointers: "XoaX.net" or L"Xoax.net"; these arrays act just like strings that use pointers. We can also initialize an array of characters with an entry list like this: { 'X', 'o', 'a', 'X', '.', 'n', 'e', 't', '\0'} or { L'X', L'o', L'a', L'X', L'.', L'n', L'e', L't', L'\0'}. This gives the same result as the previous string literals. However, we need to add a null-terminator character at the end because the compiler will not supply one in this case.

Arrays are not just for character though. We can create arrays of any type of data. For example, we can initialize an array of three ints with an entry list, like this: {32, 549, -64}. We can then access the entries with the bracket operator and we must remember to use only the index values: 0, 1, and 2.

We can also use nested array entry list literals to intialize multidimensional arrays like this for a two-dimensional 3x2 array of ints: {{3, 2}, {8, 4}, {9, 1}}

### Initialization List Literals

As a futher generalization of the entry lists that are used to initial arrays, we can use an initialization list of various data types to initialize sophisticated data types. For example, if we have a data type containing a string, an int, and a double, then we could initialize it like this: {"XoaX.net", 71, 5.96}. We will typically use intialization lists only with struct types that contain only plain old data. Also, we can nest these initialization lists just as we did for arrays.

#### Examples

Example 1

```// Declare a bool and initialize it to true.
bool bIsOver = true;
```

Example 2

```// Initialize three ints to 34, using literals with different number bases.
int iThirtyFourOct = 042;
int iThirtyFourDec = 34;
int iThirtyFourHex = 0x22;
```

Example 3

```// Initialize doubles with the value of Pi and the charge on an electron.
double dPi = 3.14159;
double dElectronCharge = 1.60217646e-19;
```

Example 4

```// Initialize two chars with the same value using a character and an int literal.
char cLowerA = 'a';
char cIntLowerA = 97;
// Initialize a wide char using a character literal.
wchar_t wLowerA = L'a';
```

Example 5

```// Initialize a char and a wide char, using universal character litersls.
char cExtAscii = '\u00e9';
wchar_t wExtAscii = L'\u00e9';
```

Example 6

```// Initialize a char string and a wide char string, using string literals.
const char* kcpXoaX = "Xoax.net";
const wchar_t* kwpXoaX = L"Xoax.net";
```

Example 7

```// Initialize a char array and a wide char array, using string literals.
const char kcaName[] = "Xoax.net";
const wchar_t kwaXoaX[] = L"Xoax.net";
```

Example 8

```// Initialize a char array and a wide char array, using entry lists.
const char kcaName[] = { 'X', 'o', 'a', 'X', '.', 'n', 'e', 't', '\0'};
const wchar_t kwaName[] = { L'X', L'o', L'a', L'X', L'.', L'n', L'e', L't', L'\0'};
```

Example 9

```// Initialize an int array with three numbers, using an entry list.
int iaNumber[] = {32, 549, -64};
```

Example 10

```// Initialize a 3x2 two-dimensional int array with nested entry lists.
// Note that only the last size must be specified in the brackets.
int iaaNumber[][2] = {{3, 2}, {8, 4}, {9, 1}};
```

Example 11

```// A defined data type with a string, an int, and a double in it..
struct SSomeData {
char* mcSomeString;
int miSomeInt;
double mdSomeDouble;
};
// The declaration and initialization of an instance
SSomeData qMyData = {"XoaX.net", 71, 5.96};
```