Literal constants, also known as immediate values, are values that are seen by the compiler as data to be converted for machine storage and do not have names or identifiers associated with them. For example, we might have a literal character constant, such as ‘A’, or a literal integral constant, such as –129, or a floating point literal, such as 3.14159.
In each case, the compiler must recognize the data type from its ASCII text representation and decide how that is to be interpreted. For example, the single quote marks are used to tell the compiler that this is to be interpreted as a character constant. Likewise, the absence of a decimal point tells the compiler that –129 should be interpreted as an integral data type, and the presence of the decimal point in 3.14159 tells the compiler that this is a floating point type. If we wanted the integral type to be interpreted as a long int instead of int, we would use the letter “L” as a suffix, because the default is int. Likewise, if we want a decimal number to be treated as float instead of the default of double, then we must use the “F” suffix.
For character literals there is a set of “escape sequences” listed in Table 2-3, page 27, to handle the non-printing characters and the characters that have special treatment in order to be included in string literals (“, ‘, and \). There are also special formats indicated for designating octal (\0nnn) and hex (\xhh) representations of characters. [Note that in the octal coding, the first character is zero – not the letter O].
Address constants are a special case in that we are asking the compiler to determine an address value that the programmer has no way of knowing directly. The programmer knows that he wants the address at which variable ctr is stored, but he has no way of knowing where ctr will be stored in memory. So, he must direct the compiler to determine that address for him using the & notation. The & in front of the variable name means “take the address of”. Later, we will see that we need addresses in order to initialize a special type of variable called a pointer.
Example:
char* cp = “Hello, Dolly\a”;
This example defines a string constant consisting of the phrase “Hello, Dolly” followed by the non-printing ASCII character that sounds the bell or beep on the computer. This string constant is NOT stored in the variable cp. Only the address of the string constant is stored in cp, because cp is a pointer to a char data type. In fact, it is the address where the letter “H” is stored in memory that will be found in pointer variable cp.
If the compiler chose to store that string constant at a memory location of Hex 00 4A 69 32, then here is what would be found in the bytes starting at that address:
00 4A 69 32 48
00 4A 69 33 65
00 4A 69 34 6C
00 4A 69 35 6C
00 4A 69 36 6F
00 4A 69 37 2C
00 4A 69 38 20
00 4A 69 39 44
00 4A 69 3A 6F
00 4A 69 3B 6C
00 4A 69 3C 6C
00 4A 69 3D 79
00 4A 69 3E 07
00 4A 69 3F 00
Note that the last character stored is a null, and that there are 14 characters stored, which is one more than the number of characters in the string between the double quote marks. That is because a null is stored at the end of the string to mark the end.
Assume that the pointer variable, cp, is stored at hex address 01 73 FF 26. If we were to examine the contents of that memory location we would find the following:
01 73 FF 26 32
01 73 FF 27 69
01 73 FF 28 4A
01 73 FF 29 00
In other words, the address of the memory location holding the letter “H” is stored there, but the bytes seem to be in reverse order. That’s because in a Win32 platform numeric values are stored in memory with the low order byte first and the high order byte last.
Do not try to run that program in Visual C++, because you will get several errors to the effect that you cannot break up that line into several lines as is done there.
The string constants that we have discussed in this topic are different from string objects of class std::string that will be discussed in Chapter 25. String objects encapsulate the character array into a class and supports it with several member functions that allow you to manipulate the strings. For now, be advised that when a character array is defined in C++, its length is forever determined and cannot be changed.