首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

integer constant

允许直接在表达式中使用整数类型的值。

句法

整型常量是表单的非左值表达式。

decimal-constant integer-suffix(optional)

(1)

?

octal-constant integer-suffix(optional)

(2)

?

hex-constant integer-suffix(optional)

(3)

?

其中

  • 十进制常数是一个非零十进制数(123456789),随后的零个或多个十进制数字(0123456789
  • 八进制常数是数字零(0),接着是零个或更多八进制数字(01234567
  • 十六进制常数是字符序列0x或字符序列0X后跟一个或多个十六进制数字(0123456789aAbBcCdDeEfF
  • 整数后缀(如果提供)可能包含以下一项或两项(如果同时提供了这两项),它们可能以任何顺序出现:
    • 无符号后缀(字符u或字符U
    • 长后缀(字符l或字符L)或长后缀(字符序列ll或字符序列LL)(自C99以来)

说明

1)十进制整数常量(以10为底,第一位数字最重要)。

2)八进制整数常量(基数8,第一位数字最重要)。

3)十六进制整数常量(基数16,第一个数字是最重要的,字母'a'到'f'代表十进制值10到15)。

以下变量初始化为相同的值:

代码语言:javascript
复制
int d = 42;
int o = 052;
int x = 0x2a;
int X = 0X2A;

整型常量的类型

整数常量的类型是该值可以匹配的第一个类型,它取决于使用哪个数字库和哪个整数后缀的类型列表。

| 整数常量允许的类型|

|:----|

| 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) |

如果整数常量的值太大而不适合后缀/基数组合所允许的任何类型,并且编译器支持扩展整数类型(如__int128),则该常量可以是扩展整数类型; 否则,该计划是不合格的。

笔记

在整数常量字母是不区分大小写的:0xDeAdBaBeU0XdeadBABEu表示相同的数目(一个例外是长长后缀,其是llLL,从未lLLl)。

没有负整数常量。诸如-1将一元减号运算符应用于由常量表示的值的表达式可能涉及隐式类型转换。

当在#if或#elif的控制表达式中使用时,所有有符号整型常量的作用就好像它们具有类型,intmax_t并且所有无符号整型常数的作用就好像它们具有类型一样uintmax_t

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

代码语言:javascript
复制
#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
}

输出:

代码语言:javascript
复制
123 = 123
0123 = 83
0x123 = 291
12345678901234567890ull = 12345678901234567890
12345678901234567890u = 12345678901234567890
9223372036854775808
-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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com