CC++基本数据类型 字节数

C\C++基本数据类型 字节数

1字节=8位

1k=1024字节=2^10

C语言类型数据所占字节数和机器字长及编译器有关系,int,long int,short int的宽度都可能随编译器而异。但有几条铁定的原则(ANSI/ISO制订的):

  1. sizeof(short int)<=sizeof(int)
  2. sizeof(int)<=sizeof(long int)
  3. short int至少应为16位(2Byte)
  4. long int至少应为32位。

类型 32位编译器 64位编译器
char 1 Byte 1 Byte
char *(指针变量 4 Byte(32bit=4Byte) 8 Byte
short int 2 Byte 2 Byte
int 4 Byte 4 Byte
unsigned int 4 Byte 4 Byte
float 4 Byte 4 Byte
double 8 Byte 8 Byte
long 4 Byte 8 Byte
long long 8 Byte 8 Byte
unsigned long 4 Byte 8 Byte

验证方式: printf("%d",sizeof(char))


确定各数据类型取值范围(头文件中含有):

(char)((unsigned char) ~0 >>1)为signed类型字符最大值。

-(char)((unsigned char) ~0 >>1)为signed类型字符最小值。

  1. ~0 //各个二进制位全部转换成1
  2. (unsigned char) ~0 //结果转换成unsigned char 类型
  3. (unsigned char) ~0 >>1 //右移一位清除符号位
  4. (char)((unsigned char) ~0 >>1) //得到signed类型最大值 ,替换char得到其他类型取值范围。

你可能感兴趣的:(c\c++,language)