?
Ce document utilise Manuel du site Web PHP chinois Libérer
允許直接在表達式中使用整數(shù)類型的值。
整型常量是表單的非左值表達式。
decimal-constant integer-suffix(optional) | (1) | |
---|---|---|
octal-constant integer-suffix(optional) | (2) | |
hex-constant integer-suffix(optional) | (3) |
其中
十進制常數(shù)是一個非零十進制數(shù)(1
,2
,3
,4
,5
,6
,7
,8
,9
),隨后的零個或多個十進制數(shù)字(0
,1
,2
,3
,4
,5
,6
,7
,8
,9
)
八進制常數(shù)是數(shù)字零(0
),接著是零個或更多八進制數(shù)字(0
,1
,2
,3
,4
,5
,6
,7
)
十六進制常數(shù)是字符序列0x
或字符序列0X
后跟一個或多個十六進制數(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àn):
無符號后綴(字符u
或字符U
)
長后綴(字符l
或字符L
)或長后綴(字符序列ll
或字符序列LL
)(自C99以來)
1)十進制整數(shù)常量(以10為底,第一位數(shù)字最重要)。
2)八進制整數(shù)常量(基數(shù)8,第一位數(shù)字最重要)。
3)十六進制整數(shù)常量(基數(shù)16,第一個數(shù)字是最重要的,字母'a'到'f'代表十進制值10到15)。
以下變量初始化為相同的值:
int d = 42;int o = 052;int x = 0x2a;int X = 0X2A;
整數(shù)常量的類型是該值可以匹配的第一個類型,它取決于使用哪個數(shù)字庫和哪個整數(shù)后綴的類型列表。
| 整數(shù)常量允許的類型|
|:----|
| 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ù)組合所允許的任何類型,并且編譯器支持擴展整數(shù)類型(如__int128
),則該常量可以是擴展整數(shù)類型; 否則,該計劃是不合格的。
在整數(shù)常量字母是不區(qū)分大小寫的:0xDeAdBaBeU
與0XdeadBABEu
表示相同的數(shù)目(一個例外是長長后綴,其是ll
或LL
,從未lL
或Ll
)。
沒有負整數(shù)常量。諸如-1
將一元減號運算符應(yīng)用于由常量表示的值的表達式可能涉及隱式類型轉(zhuǎn)換。
當在#if或#elif的控制表達式中使用時,所有有符號整型常量的作用就好像它們具有類型,intmax_t
并且所有無符號整型常數(shù)的作用就好像它們具有類型一樣uintmax_t
。
整型常量可以用在整型常量表達式中。
#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