C语言整数类型和常量
示例
有符号整数可以是以下类型(intaftershort或long可选):
signed char c = 127; /* required to be 1 byte, see remarks for further information. */ signed 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构成第三种字符类型,signedchar与unsignedchar和不同,并且签名(或不签名)取决于平台。
根据其前缀或后缀,可以使用不同的基数和不同的宽度来编写不同类型的整数常量(在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类型的常量大于该类型的常量。longlonglong
头文件<limits.h>描述了整数的限制,如下所示。其实现定义的值的大小(绝对值)应等于或大于以下所示的相同符号。