C语言整数类型和常量
示例
有符号整数可以是以下类型(intaftershort或long可选):
signed char c = 127; /* required to be 1 byte, see remarks for further information. */C99signed short int si = 32767; /* required to be at least 16 bits. */
signed int i = 32767; /* required to be at least 16 bits */
signed long int li = 2147483647; /* required to be at least 32 bits. */
signed long long int li = 2147483647; /* required to be at least 64 bits */
这些有符号整数类型均具有无符号版本。
unsigned int i = 65535;unsigned short = 2767;
unsigned char = 255;
对于所有类型,但char在signed如果假定版本signed或unsigned省略的部分。该类型char构成第三种字符类型,signed char与unsigned char和不同,并且签名(或不签名)取决于平台。
根据其前缀或后缀,可以使用不同的基数和不同的宽度来编写不同类型的整数常量(在C行话中称为文字)。
/* the following variables are initialized to the same value: */int d = 42; /* decimal constant (base10) */
int o = 052; /* octal constant (base8) */
int x = 0xaf; /* hexadecimal constants (base16) */
int X = 0XAf; /* (letters 'a' through 'f' (case insensitive) represent 10 through 15) */
小数常量始终为signed。十六进制常数以0x或开头,0X八进制常数以开头0。后两者是signed还是unsigned取决于该值是否适合带符号的类型。
/* suffixes to describe width and signedness : */long int i = 0x32; /* no suffix represent int, or long int */
unsigned int ui = 65535u; /* u or U represent unsigned int, or long int */
long int li = 65536l; /* l or L represent long int */
如果没有后缀,则常量具有适合其值的第一种类型,即十进制常量,如果可能的话,该INT_MAX类型的常量大于该类型的常量。longlong long
头文件<limits.h>描述了整数的限制,如下所示。其实现定义的值的大小(绝对值)应等于或大于以下所示的相同符号。
巨集 | 类型 | 值 |
---|---|---|
CHAR_BIT | 不是位字段的最小对象(字节) | 8 |
SCHAR_MIN | signed char | -127 / - (2 7 - 1) |
SCHAR_MAX | signed char | 127/2 7 - 1 |
UCHAR_MAX | unsigned char | 2分之255 8 - 1 |
CHAR_MIN | char | 见下文 |
CHAR_MAX | char | 见下文 |
SHRT_MIN | short int | -32767 / - (2 15 - 1) |
SHRT_MAX | short int | 32767/2 15 - 1 |
USHRT_MAX | unsigned short int | 2分之65535 16 - 1 |
INT_MIN | int | -32767 / - (2 15 - 1) |
INT_MAX | int | 32767/2 15 - 1 |
UINT_MAX | unsigned int | 2分之65535 16 - 1 |
LONG_MIN | long int | -2147483647 / - (2 31 - 1) |
LONG_MAX | long int | 2147483647/2 31 - 1 |
ULONG_MAX | unsigned long int | 2分之4294967295 32 - 1 |
巨集 | 类型 | 值 |
---|---|---|
LLONG_MIN | long long int | -9223372036854775807 / - (2 63 - 1) |
LLONG_MAX | long long int | 9223372036854775807/2 63 - 1 |
ULLONG_MAX | unsigned long long int | 2分之18446744073709551615 64 - 1 |
如果在表达式中使用类型为charsign的对象的值扩展时,的值CHAR_MIN应SCHAR_MIN与的值相同,而的值CHAR_MAX应与的值相同SCHAR_MAX。如果在表达式中使用类型的对象的值时char不进行符号扩展,则的值CHAR_MIN应为0,而的值CHAR_MAX应与的值相同UCHAR_MAX。
C99C99标准添加了一个新的标头<stdint.h>,其中包含固定宽度整数的定义。有关更深入的说明,请参见固定宽度整数示例。
以上是 C语言整数类型和常量 的全部内容, 来源链接: utcz.com/z/330667.html