从零开始学习c语言|3.2、数据类型取值范围
一、基本概念
1、bit:比特、b,计算机能识别的最小单位。
2、Byte:字节,B,内存中最小寻址单位,1B=8bit;
二、取值范围
一个字节最大能表示的数即为8个1;
即2^8-1;
代码示例
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main()
{
int i = pow(2, 32) - 1; // int整型有4个字节,即32位,32位能表示的最大数为32个1;
printf("int整型最大能表示的数为%d\n", i);
return 0;
}
运行结果
由运行结果得知,有溢出警告,这是由于int整型,默认为有符号,即最高位是符号位,所以得在int前加上无符号限定符
代码示例
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main()
{
unsigned int i = pow(2, 32) - 1; // int整型有4个字节,即32位,32位能表示的最大数为32个1;
printf("int整型最大能表示的数为%u\n", i);
return 0;
}
运行结果
另:有符号的数能表示多大的范围呢?
引进一个概念:事实上计算机是用来补码存储整数
补码:
正数的补码与正数的原码相同
负数的补码是除第一位符号位不变,其余按位取反,最后+1
故1字节能表示的最大的值为
常见数据类型的取值范围