Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 8 EXPRESSIONS AND OPERATORS

Integer Literals

Integer literals are the most commonly used literals. They are written as a sequence of decimal digits, with the following:

No decimal point

An optional suffix to specify the type of the integer

For example, the following lines show four literals for the integer 236. Each is interpreted by the compiler as a different type of integer, depending on its suffix.

236

// int

236L

// long

236U

//

unsigned

236UL

//

unsigned long

Integer type literals can also be written in hexadecimal (hex) form. The digits must be the hex digits (0 through F), and the string must be prefaced with either 0x or 0X (numeral 0, letter x).

Figure 8-1 shows the forms of the integer literal formats. Components with names in square brackets are optional.

Figure 8-1. The integer literal formats

Table 8-1 lists the integer literal suffixes. For a given suffix, the compiler will interpret the string of digits as the smallest of the corresponding integer types that can represent the value without losing data.

For example, take the literals 236 and 5000000000, neither of which has a suffix. Since 236 can be represented with 32 bits, it will be interpreted by the compiler as an int. The larger number, however, won’t fit into 32 bits, so the compiler will represent it as a long.

Table 8-1. Integer Literal Suffixes

Suffix

Integer Type

Notes

None

int, uint, long, ulong

U, u

uint, ulong

L, l

long, ulong

ul, uL, Ul, UL

ulong

lu, Lu, lU, LU

 

Using the lowercase letter l is not recommended, because it is easily mistaken for the digit 1.

Using the lowercase letter l is not recommended, because it is easily mistaken for the digit 1

204

CHAPTER 8 EXPRESSIONS AND OPERATORS

Real Literals

Literals for real numbers consist of the following:

Decimal digits

An optional decimal point

An optional exponent part

An optional suffix

For example, the following code shows various formats of literals of the real types:

float f1 = 236F; double d1 = 236.714; double d2 = .35192; double d3 = 6.338e-26;

Figure 8-2 shows the valid formats for real literals. Components with names in square brackets are optional. Table 8-2 shows the real suffixes and their meanings.

Figure 8-2. The real literal formats

Table 8-2. Suffixes for the Real Literals

Suffix

Real Type

None double

F, f float

D, d double

M, m decimal

205

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]