C Reference Manual Reading Notes: 006 Constants

    The lexical class of constants includes four different kinds of constants: integers, floating-point numbers, characters, and strings. Suck tokens are called literals in other languages to distinguish thm from objects whose value are constants(i.e., not changing) but that do not belong to lexically distinct classes. An example of these latter objects in C is enumeration constrants, which belong to the lexical class of identifiers. In this book, we use traditional C terminology of constrant for both cases.

 

    1. Integer Constants.

        Integer constants may be specified in decimal, octual, or hexadecimal notation. There are the rules for determining the radix of an integer constant:

        (a). If the integer constant begins with the letters 0X or 0x, then it is in hexadecimal notation, with the character a through f(or A through F)representing 10 through 15.

        (b). Otherwise, if it begins with digit 0, then it is in octal notation.

        (c). Otherwise, it is in decimal notation.

        An integer constant may be immediately followed by suffix letters to designate a minimum size for its type:

        (a). Letters l or L indicate a constant of type long

        (b). Letters ll or LL indicate a constant of type long long(C99)(Notes, Ll or lL is invalid)

        (c). Letters u or U indicate an unsigned type of (int, long, or long long)

        (d). The unsigned suffix may be combined with the long or long long suffix in any order, like as 100uLL or 200ULL.

        These are valid integer constants: 100, 1000L, 200ll, 0x300, 0777, 0x98FCEuLL.

 

    2. Floating-Point Constants

        Floating-point constants may be written with a decimal point, a signed exponent, or both. Standard C allows a suffix letter(floating-suffix) to designate constants of type float and long double. Without a suffix, the type of the constant is double. Letters f or F indicate a constant of type of float, and letters l or L indicate a constant of type of long double. In C99, a complex floating-point constant iw written as a floating-point constant expression involving the imaginary constant _Complex_I(or I) defined in complex.h. C99 permits floating-point constants to be expressed in hexadecimal notation; previous versions of C had only decimal floating-point constants. The hexadecimal format use the letter p to separate the fraction from the exponent because the customary e could be confused with a hexadecimal digit. The binary-exponent is a signed decimal number that represents a power of 2(not a power of 10).

        These are valid floating-point constants: 0., e21, 3.1415, .01, 1.E-3, 0.52f, 5.5E2L, 0x55fceep-5

 

    3. Character Constants

        A character constant is written by enclosing one or more characters in apostrophes. A special escape mechanism is providedto write characters or numeric values that would be inconvenient or impossible to enter directly in the source program. Standard C allows the character constant to be preceded by the letter L to specify a wide character constant. The value of a character constant is implementation-defined if:

        (a). there is no corresponding character int the execution character set.

        (b). more than a single execution character appears in the constant, or

        (c). a numeric escape has a value not represented in the execution character set.

        Character constants not preceded by the letter L have type int. And wide character constants that designated by the prefix letter L have type wchar_t.

        Examples of  single-character constants along with their(decimal) values under the ASCII encoding:

            'a',  '/r',  ' ',  '/'',  '"',  '/0',  '/377',  '23',  '//'

 

    4. String Constants

        A string constant is a (possibly empty) sequence of characters enclosed in double quotes. The same escape mechanism provided for character constants can be used to express the characters in the string. Standard C allows the string constant to be preceded by the letter L to specify a wide string constant.

        For each nonwide string constant of n characters, at run time there will be a statically allocated block of n+1 characters whose first n characters are the character from the string and whose last character is the null character, '/0'. This block is the value of the string constant and its type is char[n+1]. Wide sting constants similarly become n wide characters followed by a null wide character and have type wchar-t[n+1].

        If a string constant appears anywhere except as an argument to the address operator &, an argument to the sizeof operator, or as an initializer of a character array, then the usual array conversions come into play, changing the string from an array of characters to a pointerto the first character in the string.

 

    5. Escape Characters

        a -- alert,                b--backspace,               f--form feed,               n--new line,               r--carriage return

        t--horizontal tab,    v--vertical tab,              /--backslash,               '--single quote,         "--double quote

        ?--question mark

        numeric escape code: '/004', '/006', '/xabc', etc.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值