名稱空間
變體
操作

整型常量

來自 cppreference.com
< c‎ | 語言

允許在表示式中直接使用整型值。

目錄

[編輯] 語法

整型常量是以下形式的非左值表示式:

decimal-constant integer-suffix (可選) (1)
octal-constant integer-suffix (可選) (2)
hex-constant integer-suffix (可選) (3)
binary-constant integer-suffix (可選) (4) (自 C23 起)

其中

  • decimal-constant 是一個非零十進位制數字 (1, 2, 3, 4, 5, 6, 7, 8, 9),後跟零個或多個十進位制數字 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  • octal-constant 是數字零 (0),後跟零個或多個八進位制數字 (0, 1, 2, 3, 4, 5, 6, 7)
  • hex-constant 是字元序列 0x 或字元序列 0X,後跟一個或多個十六進位制數字 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F)
  • binary-constant 是字元序列 0b 或字元序列 0B,後跟一個或多個二進位制數字 (0, 1)
  • integer-suffix,如果提供,可以包含以下之一(除了無符號字首可以與其他字首組合;如果使用兩個字尾,它們可以以任何順序出現)
  • unsigned-suffix(字元 u 或字元 U
  • long-suffix(字元 l 或字元 Llong-long-suffix(字元序列 ll 或字元序列 LL(C99 起)
  • bit-precise-int-suffix(字元序列 wb 或字元序列 WB(C23 起)

可選的單引號 (') 可以插入數字之間作為分隔符。它們被編譯器忽略。

(自 C23 起)

[編輯] 解釋

1) 十進位制整型常量(基數 10,第一個數字是最重要的)。
2) 八進位制整型常量(基數 8,第一個數字是最重要的)。
3) 十六進位制整型常量(基數 16,第一個數字是最重要的,字母 af 代表十進位制值 10 到 15)。
4) 二進位制整型常量(基數 2,第一個數字是最重要的)。

以下變數被初始化為相同的值

int d = 42;
int o = 052;
int x = 0x2a;
int X = 0X2A;
int b = 0b101010; // C23

以下變數也被初始化為相同的值

unsigned long long l1 = 18446744073709550592ull; // C99
unsigned long long l2 = 18'446'744'073'709'550'592llu; // C23
unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C23
unsigned long long l4 = 184467'440737'0'95505'92LLU; // C23

[編輯] 整型常量的型別

整型常量的型別是根據所使用的數字基數和 integer-suffix 列表中的第一個能容納該值的型別。

整型常量允許的型別
字尾 十進位制基數 其他基數
無後綴 int

long int
unsigned long int (C99 前)
long long int (C99 起)

int

unsigned int
long int
unsigned long int
long long int(C99 起)
unsigned long long int(C99 起)

uU unsigned int

unsigned long int
unsigned long long int(C99 起)

unsigned int

unsigned long int
unsigned long long int(C99 起)

lL long int

unsigned long int(C99 前)
long long int(C99 起)

long int

unsigned long int
long long int(C99 起)
unsigned long long int(C99 起)

l/Lu/U 都存在 unsigned long int

unsigned long long int(C99 起)

unsigned long int

unsigned long long int(C99 起)

llLL long long int(C99 起) long long int(C99 起)

unsigned long long int(C99 起)

ll/LLu/U 都存在 unsigned long long int(C99 起) unsigned long long int(C99 起)
wbWB _BitInt(N),其中寬度 N 是大於 1 的最小 N,可以容納該值和符號位(C23 起) _BitInt(N),其中寬度 N 是大於 1 的最小 N,可以容納該值和符號位(C23 起)
wb/WBu/U 都存在 unsigned _BitInt(N),其中寬度 N 是大於 0 的最小 N,可以容納該值(C23 起) unsigned _BitInt(N),其中寬度 N 是大於 0 的最小 N,可以容納該值(C23 起)

如果整型常量的值太大,無法容納於任何字尾/基數組合允許的型別中,它沒有後綴 wbWBuwbUWB(C23 起)並且編譯器支援擴充套件整型(例如 __int128),該常量可以被賦予擴充套件整型;否則,程式格式錯誤。

[編輯] 注意

整型常量中的字母不區分大小寫:0xDeAdBaBeU0XdeadBABEu 代表相同的數字(一個例外是 long-long-suffix,它是 llLL,從不為 lLLl(C99 起)

沒有負整型常量。諸如 -1 的表示式將一元減運算子應用於常量表示的值。

當在 #if#elif 的控制表示式中使用時,所有有符號整型常量都表現為具有型別 intmax_t,所有無符號整型常量都表現為具有型別 uintmax_t

(C99 起)

整型常量可以在整型常量表達式中使用。

由於最大吞噬原則,以 eE 結尾的十六進位制整型常量,當後跟運算子 +- 時,必須在原始碼中用空格或括號將它們與運算子分開

int x = 0xE+2;   // error
int y = 0xa+2;   // OK
int z = 0xE +2;  // OK
int q = (0xE)+2; // OK

否則,將形成一個無效的預處理數字標記,導致進一步的分析失敗。

[編輯] 示例

#include <inttypes.h>
#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 a 64-bit type (unsigned long long or possibly unsigned long)
    // even without a long suffix
    printf("12345678901234567890u = %"PRIu64"\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", -9223372036854775808ull );
    // unary minus applied to unsigned value subtracts it from 2^64,
    // this gives unsigned 9223372036854775808
 
    printf("%lld\n", -9223372036854775807ll - 1);
    // correct way to form signed value -9223372036854775808
}

輸出

123 = 123
0123 = 83
0x123 = 291
12345678901234567890ull = 12345678901234567890
12345678901234567890u = 12345678901234567890
9223372036854775808
-9223372036854775808

[編輯] 參考文獻

  • C23 標準 (ISO/IEC 9899:2024)
  • 6.4.4.1 整型常量 (p: 57-60)
  • C17 標準 (ISO/IEC 9899:2018)
  • 6.4.4.1 整型常量 (p: 45-46)
  • C11 標準 (ISO/IEC 9899:2011)
  • 6.4.4.1 整型常量 (p: 62-64)
  • C99 標準 (ISO/IEC 9899:1999)
  • 6.4.4.1 整型常量 (p: 54-56)
  • C89/C90 標準 (ISO/IEC 9899:1990)
  • 3.1.3.2 整型常量

[編輯] 另請參閱

C++ 文件,關於 整型字面量