6.C语言-数据的类型大小
1字节= 0 0 0 0 0 0 0 0
一个字节 = 8 位
-
1024 = 1KB
-
1024KB = 1MB
-
1024MB = 1G
-
1024GB = 1TB
-
1024TB = 1PB
-
1024pB = 1EB
-
1024EB = 1ZB
-
1024ZB = 1YB
-
整形
- int - 为4个字节 相当于 32bit
- short - 2字节 = 16字节
- long - 4~8字节 (在windows中占4字节。linux的32位占4字节,64位占8字节)
- long long
int main() {
//短整型
short a = 10;
printf("%d\n", a);
//整形 int
int b = 100;
printf("%d\n", b);
//长整形 long
long c = 1000L;
printf("%ld\n", c);
//超长整形 long long (windows 8个字节(19位))
long long d = 10000LL;
printf("%lld\n", d);
//使用sizeof测量每种数据类型占用多少字节
//siezof(变量名/数据类型)
printf("%zu\n", sizeof(short));
printf("%zu\n", sizeof(a));
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(b));
printf("%zu\n", sizeof(long));
printf("%zu\n", sizeof(c));
printf("%zu\n", sizeof(long long));
printf("%zu\n", sizeof(d));
return 0;
}
- 无符号整数
//有符号整数(有正负) 、可省略不行
signed int e = -100;
printf("%d\n", e);
//使用unsigned 时,打印需要使用 %u
//无符号整数(无负数)
unsigned int f = 999;
printf("%u\n", f);
//short:-32768~32768
//无符号的short: 0~65535
unsigned short num1 = 65535;
printf("%u", num1);
return 0;
}
+小数
- float - 占4字节
- double - 占8字节
int main() {
//float 单精度小数(精确度小数点后6位)windows占4个字节(38位)
float a = 3.14F;
printf("%f\n", a); //默认保存6位小数
printf("%.2f\n", a); //保存2位小数
//double 双精度小数(精确度小数后15位)windows占8字节(308位)
double b = 1.78;
printf("%lf\n", b);
printf("%.2lf\n", b);
//long double 高精度小数 (精确后小数点18~19位) windows占8个字节
long double c = 3.1415926L;
printf("%.2lf\n", c);
//小数的数据类型无法 和 unsigned 结合使用的
return 0;
}
- 字符 char
- 取值范围:ASCII码表中的字母、数字、英文 (没有中文)
int main() {
//定义char 在windows中占1个字节
char c1 = 'a';
printf("%c\n", c1);
char c2 = '1';
printf("%c\n", c2);
char c3 = 'A';
printf("%c\n", c3);
char c4 = '.';
printf("%c\n", c4);
return 0;
}