?
This document uses PHP Chinese website manual Release
允許直接在表達(dá)式中使用整數(shù)類(lèi)型的值。
整型常量是表單的非左值表達(dá)式。
decimal-constant integer-suffix(optional) | (1) | |
---|---|---|
octal-constant integer-suffix(optional) | (2) | |
hex-constant integer-suffix(optional) | (3) |
其中
十進(jìn)制常數(shù)是一個(gè)非零十進(jìn)制數(shù)(1
,2
,3
,4
,5
,6
,7
,8
,9
),隨后的零個(gè)或多個(gè)十進(jìn)制數(shù)字(0
,1
,2
,3
,4
,5
,6
,7
,8
,9
)
八進(jìn)制常數(shù)是數(shù)字零(0
),接著是零個(gè)或更多八進(jìn)制數(shù)字(0
,1
,2
,3
,4
,5
,6
,7
)
十六進(jìn)制常數(shù)是字符序列0x
或字符序列0X
后跟一個(gè)或多個(gè)十六進(jìn)制數(shù)字(0
,1
,2
,3
,4
,5
,6
,7
,8
,9
,a
,A
,b
,B
,c
,C
,d
,D
,e
,E
,f
,F
)
整數(shù)后綴(如果提供)可能包含以下一項(xiàng)或兩項(xiàng)(如果同時(shí)提供了這兩項(xiàng)),它們可能以任何順序出現(xiàn):
無(wú)符號(hào)后綴(字符u
或字符U
)
長(zhǎng)后綴(字符l
或字符L
)或長(zhǎng)后綴(字符序列ll
或字符序列LL
)(自C99以來(lái))
1)十進(jìn)制整數(shù)常量(以10為底,第一位數(shù)字最重要)。
2)八進(jìn)制整數(shù)常量(基數(shù)8,第一位數(shù)字最重要)。
3)十六進(jìn)制整數(shù)常量(基數(shù)16,第一個(gè)數(shù)字是最重要的,字母'a'到'f'代表十進(jìn)制值10到15)。
以下變量初始化為相同的值:
int d = 42;int o = 052;int x = 0x2a;int X = 0X2A;
整數(shù)常量的類(lèi)型是該值可以匹配的第一個(gè)類(lèi)型,它取決于使用哪個(gè)數(shù)字庫(kù)和哪個(gè)整數(shù)后綴的類(lèi)型列表。
| 整數(shù)常量允許的類(lèi)型|
|:----|
| suffix | decimal bases | hexadecimal or octal bases |
| no suffix | int long int unsigned long int (until C99) long long int (since C99). | int unsigned int long int unsigned long int long long int(since C99) unsigned long long int(since C99). |
| u or U | unsigned int unsigned long int unsigned long long int(since C99). | unsigned int unsigned long int unsigned long long int(since C99). |
| l or L | long int unsigned long int(until C99) long long int(since C99). | long int unsigned long int long long int(since C99) unsigned long long int(since C99). |
| both l/L and u/U | unsigned long int unsigned long long int(since C99). | unsigned long int unsigned long long int(since C99). |
| ll or LL | long long int(since C99) | long long int(since C99) unsigned long long int(since C99). |
| both ll/LL and u/U | unsigned long long int(since C99) | unsigned long long int(since C99) |
如果整數(shù)常量的值太大而不適合后綴/基數(shù)組合所允許的任何類(lèi)型,并且編譯器支持?jǐn)U展整數(shù)類(lèi)型(如__int128
),則該常量可以是擴(kuò)展整數(shù)類(lèi)型; 否則,該計(jì)劃是不合格的。
在整數(shù)常量字母是不區(qū)分大小寫(xiě)的:0xDeAdBaBeU
與0XdeadBABEu
表示相同的數(shù)目(一個(gè)例外是長(zhǎng)長(zhǎng)后綴,其是ll
或LL
,從未lL
或Ll
)。
沒(méi)有負(fù)整數(shù)常量。諸如-1
將一元減號(hào)運(yùn)算符應(yīng)用于由常量表示的值的表達(dá)式可能涉及隱式類(lèi)型轉(zhuǎn)換。
當(dāng)在#if或#elif的控制表達(dá)式中使用時(shí),所有有符號(hào)整型常量的作用就好像它們具有類(lèi)型,intmax_t
并且所有無(wú)符號(hào)整型常數(shù)的作用就好像它們具有類(lèi)型一樣uintmax_t
。
整型常量可以用在整型常量表達(dá)式中。
#include <stdio.h> int main(void){ printf("123 = %d\n", 123); printf("0123 = %d\n", 0123); printf("0x123 = %d\n", 0x123); printf("12345678901234567890ull = %llu\n", 12345678901234567890ull); // the type is unsigned long long even without a long long suffix printf("12345678901234567890u = %llu\n", 12345678901234567890u ); // printf("%lld\n", -9223372036854775808); // ERROR// the value 9223372036854775808 cannot fit in signed long long, which is the// biggest type allowed for unsuffixed decimal integer constant printf("%llu\n", -9223372036854775808u ); // unary minus applied to unsigned value subtracts it from 2^64, // this gives 9223372036854775808 printf("%lld\n", -9223372036854775807 - 1); // // correct way to represent the value -9223372036854775808}
輸出:
123 = 1230123 = 830x123 = 29112345678901234567890ull = 1234567890123456789012345678901234567890u = 123456789012345678909223372036854775808-9223372036854775808
C11 standard (ISO/IEC 9899:2011):
6.4.4.1 Integer constants (p: 62-64)
C99 standard (ISO/IEC 9899:1999):
6.4.4.1 Integer constants (p: 54-56)
C89/C90 standard (ISO/IEC 9899:1990):
3.1.3.2 Integer constants