整型

char

计算机存储的最小单位是字节( Byte) ,一个字节通常是8个bit。 C语言规定char型占一个字节的存储空间。

如果这8个bit按无符号整数来解释,则取值范围是0~255,如果按有符号整数来解释,则取值范围是-128~127。 C语言规定了signed和unsigned两个关键字, unsigned char型表示无符号数, signed char型表示有符号数。 

 注意:     

char在缺省的情况下,可能是singned char,也可能是unsigned char,这取决于编译器。这意味着char在不同的机器上可能有不同的取值范围,所以,只有当程序使用的变量值位于singned char和unsigned char的交集中才能移植。

整型变量所占字节数

除了char型在C标准中明确规定占一个字节之外,其它整数类型占几个字节都是Implementation Defined。通常的编译器实现遵守ILP32或LP64规范,如下表所示:

类型

ILP32(位数)

LP64(位数)

char

8

8

short

16

16

int

32

32

long

32

64

long long

64

64

指针

32

64

ILP32这个缩写的意思是int( I)、 long( L)和指针( P)类型都占32位,通常32位计算机的C编译器采用这种规范, x86平台的gcc也是如此。 LP64是指long( L)和指针占64位,通常64位计算机的C编译器采用这种规范。

为了得到某个类型或某个变量在特定平台上的准确大小,可以使用 sizeof 运算符。表达式 sizeof(type) 得到对象或类型的存储字节大小。下面的实例演示了获取 int 类型的大小: 

#include <stdio.h>
#include <limits.h>

int main()

{
   printf("int 存储大小 : %lu \n", sizeof(int));  
   return 0;
}

进制表示

其实在C语言中除了10进制表示,也可以用八进制和十六进制的整数常量,八进制整数常量以0开头,后面的数字只能是0~7,例如022,因此十进制的整数常量就不能以0开头了,否则无法和八进制区分。十六进制整数常量以0x或0X开头,后面的数字可以是0~9、 a~f和A~F。

后缀

整数常量还可以在末尾在加u或U表示“unsigned”,加l或L表示“long”,加ll或LL表示“long long” ,但是不同进制加后缀的含义是不同的,具体如下:

后缀

十进制常量

八进制或十六进制常量

int
long int
long long int

int
unsigned int
long int
unsigned long int
long long int
unsigned long long int

u或U

unsigned int
unsigned long int
unsigned long long int

unsigned int
unsigned long int
unsigned long long int

l或L

long int
long long int

long int
unsigned long int
long long int
unsigned long long int

既有u或U,又有l或L

unsigned long int
unsigned long long int

unsigned long int
unsigned long long int

ll或LL

long long int

long long int
unsigned long long int

既有u或U,又有ll或LL

unsigned long long int

unsigned long long int

对于给定一个整数常量,找到第一个足够长的类型表示即可:比如1234U,使用int类型表示即可。
整型包括有符号和无符号的charintlonglong long,还包括的Bit-field,此外,枚举常量就是int型的,所以也属于整型。

posted @ 2018-04-07 10:19  刘-皇叔  阅读(957)  评论(0)    收藏  举报