Literals


Other than Decimals, each of the preceding data types has literals that can be used for things such as initializing variables or as constants. In the preceding programs, I have shown many different literals. In this section, I go over them in more detail.

Numeric Literals

Numeric literals come in five flavors:

  • Octal numbers

  • Integer numbers

  • Hexadecimal numbers

  • Decimal numbers

  • Exponential numbers

Octal numbers are hardly ever used anymore. They mainly are sticking around just for backward compatibility with some ancient programs. They are base-8 numbers and thus made up of the numbers 0 through 7. All octal numbers start with a 0. Some examples are as follows:

0123 (an integer value of 83)

01010 (an integer value of 520)

You need to be aware of octal numbers because if you mistakenly start an integer number with a 0, then the compiler will happily treat it as an octal number. For example, if you type in 0246, the compiler will think its value is equivalent to the integer value 166.

Integer numbers are straightforward. They are simply whole numbers. Some examples are as follows:

1234

-1234

+1234

The symbols - and + are not actually part of the number but, in fact, are unary operators that convert the whole number into a negative or positive number. The + unary operator is assumed, so 1234 and +1234 mean the same thing.

Hexadecimal numbers are the most complex of the numeric constants. They are base-16 numbers and are made up of the numbers 0 through 9 and the letters A through F (or a through f, as case does not matter). The letters represent the numbers 10 through 15. A hexadecimal literal always starts with "0x". Some examples of hexadecimal numbers are as follows:

0x1234 (an integer value of 4660) 0xabcd (an integer value of 43981)

Decimal numbers are the same as integer numbers, except they also contain a decimal and a fractional portion. They are used to represent real numbers. Some examples are as follows:

1.0

3.1415

-1.23

Just like integer numbers, the minus symbol (-) is a unary operator and not part of the decimal number.

The last numeric literals are the exponential numbers. They are similar to decimal numbers except that along with the decimal—or more accurately, the mantissas—is the exponent, which tells the compiler how many times to multiply or divide the mantissa by 10. When the exponent is positive, the mantissa is multiplied by ten exponent times. If the exponent is negative, the mantissa is divided by ten exponent times. Some examples are as follows:

1.23e4 (a decimal value of 12300.0)

1.23e-4 (a decimal value of 0.000123)

An interesting feature that comes along with Managed C++ is that numeric literals are also objects. This means that they also have the ToString() method. Listing 2-10 shows a numeric literal object in action. Note that you need to surround the numeric literal with brackets.

Listing 2-10: Numeric Literals in Action

start example
 #using <mscorlib.dll> using namespace System; Int32 main(void) {     Console::WriteLine (   010 );  // An Octal 10 is a base-10 8     Console::WriteLine (  -010 ); // Negative Octal 10 is a base-10 -8     Console::WriteLine (  0x10 );  // A Hex 10 is a base-10 16     Console::WriteLine ( -0x10 ); // Negative Hex 10 is a base-10 -16     // This is kind of neat. Number literals are objects too!     Console::WriteLine ( (1234567890).ToString()  );     Console::WriteLine ( (0xABCDEF).ToString("X") );     return 0; } 
end example

Figure 2-11 shows the results of this little program.

click to expand
Figure 2-11: Results of IntegerLiteral.exe

Boolean Literals

There are only two boolean literals: the values true and false.

Like numeric literals, boolean literals are objects in Managed C++. Thus, they too provide the ToString() method. Listing 2-11 shows a boolean literal object in action.

Listing 2-11: Boolean Literals in Action

start example
 #using <mscorlib.dll> using namespace System; Int32 main(void) {     // This is kind of neat. Boolean literals are objects too!    Console::WriteLine ( true.ToString () );    Console::WriteLine ( false.ToString () );    return 0; } 
end example

Figure 2-12 shows the results of this little program.

click to expand
Figure 2-12: Results of BooleanLiteral.exe

Character Literals

Managed C++ provides two different types of character literals:

  • Character

  • Escape sequence

Character literals are the most basic form and are simply a printable letter, number, or symbol enclosed in single quotes. These literals can be placed in both Byte types (or any other integer type, for that matter) and Char types. Here are a few examples:

 'A'     '0'     '+' 

Escape sequences are a little more elaborate and come in a few flavors. Like the character literal form, escape sequences are placed within single quotes. The first character within the quotes is always a backslash (\). After the backslash will be a character such as the ones shown in Table 2-7, an octal number, or an x followed by a hexadecimal number. The octal or hexadecimal numbers are the numeric equivalent of the character you want the literal to represent.

Table 2-7: Special Escape Sequences

ESCAPE SEQUENCE

CHARACTER

\?

Question mark

\'

Single quote

\"

Double quote

\\

Backslash

\0

Null

\a

Bell or alert

\b

Backspace

\f

Form feed

\n

New line

\r

Carriage return

\t

Tab

\v

Vertical tab

All the character literal types can be prefixed with the letter L to tell the compiler to create a Unicode equivalent of the character literal. Remember that Unicode characters are 16 bits, so they will not fit in Byte types; instead, they should be placed in Char types.

Listing 2-12 is a program showing character literals in action.

Listing 2-12: Character Literals in Action

start example
 #using <mscorlib.dll> using namespace System; Int32 main(void) {     Byte a = 'a';        // character 'a'     Char b = L 'b';      // Unicode 'b'     Byte t = '\t';       // tab escape     Char s = L'\\';      // Unicode backslash escape     Byte d = '\45';      // octal escape     Char e = L'\x0045';  // Unicode hexadecimal escape     Console::WriteLine ( a ); // displays numeric equiv of 'A'     Console::WriteLine ( b ); // displays the letter 'b'     Console::WriteLine ( t ); // displays numeric equiv of tab     Console::WriteLine ( s ); // displays backslash     Console::WriteLine ( d ); // displays integer equiv of octal 45     Console::WriteLine ( e ); // displays the letter 'e'     return 0; } 
end example

Figure 2-13 shows the results of this little program.

click to expand
Figure 2-13: Results of CharLiteral.exe

String Literals

Managed string literals are simply character strings enclosed in double quotes and prefixed by the letter S.

You can also create literal strings without a prefix, creating an unmanaged string literal or, when you prefix with the letter L, creating an unmanaged Unicode string literal. Both of these will work with Managed C++ but suffer in performance, as they have to get converted at runtime to managed Strings before they can be used with the String type.

By the way, the escape sequences shown previously also work within Strings. You must be careful to avoid too many characters after the backslash being taken as the escape sequence. Realistic examples of this are difficult with the Latin alphabet, but this illustrates the point:

 String *s1 = S"\x61";  // a String *s2 = S"\x611"; // is NOT a1 but a Unicode hexadecimal escape of 611 

Listing 2-13 is a program showing string literals in action.

Listing 2-13: String Literals in Action

start example
 #using <mscorlib.dll> using namespace System; Int32 main(void) {     String *a = S"Managed String"; // The preferred String Literal for Managed C++     String *b = L"Unicode String"; // Unmanaged needs runtime conversion     String *c =  "Unmanaged String"; // Unmanaged need runtime conversion     Console::WriteLine(a);     Console::Write Line (b);     Console::WriteLine(c);     return 0; } 
end example

Figure 2-14 shows the results of this little program.

click to expand
Figure 2-14: Results of StringLiteral.exe




Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169

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