Literals

 < Day Day Up > 



You often need to represent data “literally” in your program. The following statement gives an example:

int i = 25;

The integer value 25 is being assigned to the newly declared integer variable named i. When numbers, characters, and strings of characters appear directly in source code, like the 25 does in this example, they are called literals. Here, the value 25 is known as a decimal integer literal.

Integer Literals

There are three types of integer literals: decimal, octal, and hexadecimal.

Decimal

You have already seen an example of a decimal integer literal above. Decimal literals must start with a non-zero digit. Digits following the first can be zero or non-zero. Decimal literals can have a suffix of L or l, or U or u, attached marking them as long or unsigned. You can combine the suffixes to indicate a decimal literal is both unsigned and long. Decimal literals have no decimal point. Here are a few more examples of decimal literals:

123L 34 2887934567ul 28l

Octal

Octal literals are formed beginning with a zero digit prefix followed by octal digits. The octal digits include the integers 0 - 7. They can have the same suffixes attached as their decimal counterparts. Examine the following octal literal examples:

0123L 034 010 0673467UL

Hexadecimal

Hexadecimal literals are formed beginning with the zero digit and the letter x prefix. The x can be lowercase or uppercase. The prefix is followed by hexadecimal digits. The hexadecimal digits include the digits 0-9 and the letters a-f or A-F. Hexadecimal literals can be suffixed like their octal and decimal counterparts. Here are a few examples:

0x123L 0X1a2b3c4dL 0x2887934567ul 0x28l

A Word of Caution

Always be aware of the size of your integer literal and how it is being used. If you are assigning it to a variable make sure the variable’s type is large enough to hold the literal’s complete value. For example, the following statement attempts to assign a large literal to a variable whose type isn’t big enough to sufficiently store it:

unsigned short small_fry = 0xffffffff;

In this case, small_fry is an unsigned short which is only two bytes long. The literal being assigned is four bytes long. Assigning big literals to objects with insufficient storage capacity will result in truncation of the literal value and no compiler warning. The following statement will work fine:

unsigned int tough_guy = 0xffffffff;

In this case tough_guy is an unsigned integer type that’s four bytes long.

Character Literals

Character literals are formed by enclosing characters between single quotes. Essentially four things can appear between the single quotes: a single character or multiple characters, a simple escape sequence, an octal escape sequence, or a hexadecimal escape sequence. Escape sequences provide a way of representing special characters or characters not otherwise represented in the implementation character set.

Single Character Literals

Single character literals are formed by enclosing a single character between single quotes. Single character literals are of type char. Here are a few examples of single character literals:

 ‘a’ ‘1’ ‘t’ ‘?’

Multiple Character Literals

Multiple character literals are formed by enclosing more than one character between single quotes. Multiple character literals have the type and the numeric value they contain is implementation dependent. You normally don’t use multiple character literals. For example, the following statement attempts to assign a multiple character literal to a char variable:

char c = ‘Help’;

In this example the multiple character literal consists of four chars. Each char takes up a byte of storage. The char variable is one byte. There’s just not enough space. After making this assignment, printing the variable c results in the character p being printed to the screen.

What’s happening here is that value of the characters between the single quotes is being assigned to the char variable. If you change the type from char to int there will be no loss of data in the conversion. The following source code gives an example:

int c = ‘Help’;

Now when the assignment is made there is no loss of data. But what value will the variable c contain? It is not the four characters ‘Help’, rather, it is the value of the 32-bit word that contains the ASCII value of ‘H’ in the most significant byte, followed by the ASCII value of ‘e’ in the next byte, followed by the ASCII value of ‘l’ in the third byte, followed lastly by the ASCII value of ‘p’ in the least significant byte.

Figure 5-6 illustrates how the integer value of ‘Help’ can be manually calculated. Determine the hexadecimal value for each character from an ASCII table. The conversion from hexadecimal to binary is then a straightforward operation. The binary positional values are then calculated and added yielding a total of 1,214,606,448.

The important thing to remember about multiple character literals is that they are not strings of characters although a quick glance misleads the uninitiated to believe otherwise.

click to expand
Figure 5-6: Integer Value of Character Literal ‘Help’

Escape Sequences

Character literals can also be represented as escape sequences. An escape sequence begins with the backslash character ‘\’. Three different things can follow the backslash character: a character that together with the backslash results in a simple escape, a series of octal digits resulting in an octal escape, or a series of hexadecimal digits resulting in a hexadecimal escape.

Simple Escape Sequences

Table 5-2 lists the simple escapes.

Table 5-2: Simple Escape Sequences

Escape Sequence

Meaning

\n

Newline

\t

Horizontal Tab

\v

Vertical Tab

\b

Backspace

\r

Carriage Return

\f

Form Feed

\a

Alert

\\

Backslash

\?

Question Mark

\’

Single Quote

\”

Double Quote

Simple escape sequences must be enclosed in single quotes. The following statement gives an example of a simple escape in use:

char c = ‘\’’; 

The character being assigned to the variable c is the single quote.

Octal Escape Sequences

Octal escape sequences are formed with the backslash character followed by up to three octal digits. Octal escape sequences must be enclosed in single quotes. The following statement gives an example of an octal escape:

char c = ‘\230’; 

Printing the variable c with this octal value results in the character Ò being printed to the screen on a Macintosh.

Hexadecimal Escape Sequences

Hexadecimal escape sequences are formed with the backslash character followed by the character x, then a sequence of hexadecimal digits. Hexadecimal escape sequences must be enclosed in single quotes. Here’s an example:

char c = ‘\xC0’; 

Printing the variable c with this hexadecimal value results in the character ¿ being printed to the screen.

Floating Point Literals

Figure 5-7 dissects a floating point literal.

click to expand
Figure 5-7: Parts of a Floating Point Literal

Well...everything’s not optional at the same time. There are some rules. The integer part or the fraction part can be omitted but not both at the same time. The (decimal point) or (the letter e and the exponent) can be omitted. I added the parenthesis for clarification of grouping.

The natural type for a floating point literal is double. It can be changed to float or long double by adding the suffix F or L respectively. Suffixes can be in upper or lower case. Here are a few examples of floating point literals:

2e+3

-2e3f

.357E5

5.0L

Notice that you can sign the integer part.

String Literals

String literals are sequences of characters enclosed in double quotes. String literals are automatically terminated with a null character ‘\0’. This is important in that what differentiates strings from ordinary arrays of characters is the presence of the null terminator at the end of each string. The null terminator is used by string processing programs to determine the end of the string. Here’s another very important characteristic of string literals to keep in mind: A string literal is an “lvalue” as opposed to all the other types of literals which are “rvalues”. Let us take a closer look at string literals.

The following statement assigns a string literal to an array:

char char_array[] = “Hello World!”;

The next statement prints the contents of char_array to the screen:

cout<<char_array<<endl;

The following statement will print the size of char_array to the screen:

cout<<sizeof char_array<<endl;

The value printed by the previous statement is 13. This leads to an important property of strings: The size of a string is the number of characters the string contains plus the null terminator.

To embed double quotes in a string literal use an escape sequence. The following statement gives an example:

cout<<“The man yelled, \”Run Jimmy, run!\””<<endl

Boolean Literals

The boolean values 1 and 0 are represented by the boolean literals true and false. The following statement gives an example of their use:

bool keep_going = true;

In this example, a boolean variable named keep_going is declared and assigned the value 1 or true. The following statement prints the value of the variable keep_going to the screen:

cout<<keep_going<<endl;

The following statement will print the values of the boolean literals true and false to the screen:

cout<<true<<“ “<<false<<endl;



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net