找回密码
 会员注册
查看: 9|回复: 0

izeof(float)(314是float还是double)

[复制链接]

2

主题

0

回帖

7

积分

新手上路

积分
7
发表于 2024-12-30 23:37:28 | 显示全部楼层 |阅读模式
对于c语言来说,数据类型是一个很重要的概念和知识点,它涉及到的是内存的空间,这在和硬件交互的时候是非常重要的。 K&R给出了7个数据类型相关的关键字,分别是:int、long、short、unsigned、char、float、double。后来C90增加了2个关键字,分别是:signed、void。再后来,C99又增加了3个关键字,分别是:_Bool、_Complex、_Imaginary。 整体来说,我们可以分为2个系列:分别是整数类型和浮点数类型。 下面就依次来说说了(环境:win7 64bit 、VS2015): int(有符号整型)占用的字节数:printf("int:bytes %d;bit %dn", sizeof(int), sizeof(int) * 8);int:bytes 4;bit 32取值范围:printf("int:MIN %d;MAX %dn", INT_MIN, INT_MAX);int:MIN -2147483648;MAX 2147483647 即-2^31~2^31-1格式说明符:%d(十进制)、%o(八进制)、%x(十六进制)unsigned int(无符号整型)占用的字节数:printf("unsigned int:bytes %d;bit %dn", sizeof(unsigned int), sizeof(unsigned int) * 8);unsigned int:bytes 4;bit 32取值范围:printf("unsigned int:MIN is 0;MAX %un", UINT_MAX);unsigned int:MIN is 0;MAX 4294967295 即0~2^32格式说明符:%u(十进制)char(有符号字符型)占用的字节数:printf("char:bytes %d;bit %dn", sizeof(char), sizeof(char) * 8);char:bytes 1;bit 8取值范围:printf("char:MIN %d;MAX %dn", CHAR_MIN, CHAR_MAX);char:MIN -128;MAX 127 即-2^7~2^7-1格式说明符:%d(十进制)unsigned char(无符号字符型)占用的字节数:printf("unsigned char:bytes %d;bit %dn", sizeof(unsigned char), sizeof(unsigned char) * 8);unsigned char:bytes 1;bit 8取值范围:printf("unsigned char:MIN is 0;MAX %dn", UCHAR_MAX);unsigned char:MIN is 0;MAX 255 即0~2^8-1格式说明符:%d(十进制)short(有符号短整型)占用的字节数:printf("short:bytes %d;bit %dn", sizeof(short), sizeof(short) * 8);short) 2;bit 16取值范围:printf("short :MIN %hd;MAX %hdn", SHRT_MIN, SHRT_MAX);short :MIN -32768;MAX 32767 即-2^15~2^15-1格式说明符:%hd(十进制)unsigned short(无符号短整型)占用的字节数:printf("unsigned short:bytes %d;bit %dn", sizeof(unsigned short), sizeof(unsigned short) * 8);unsigned short:bytes 2;bit 16:bytes 2;bit 16取值范围:printf("unsigned short:MIN is 0;MAX %dn", USHRT_MAX);unsigned short:MIN is 0;MAX 65535 即0~2^16-1格式说明符:%hd(十进制)long(有符号长整型)占用的字节数:printf("long:bytes %d;bit %dn", sizeof(long), sizeof(long) * 8);long:bytes 4;bit 32取值范围:printf("long :MIN %ld;MAX %ldn", LONG_MIN, LONG_MAX);long :MIN -2147483648;MAX 2147483647 即-2^31~2^31-1格式说明符:%ldunsigned long(无符号长整型)占用的字节数:unsigned long:bytes %d;bit %dn", sizeof(unsigned long), sizeof(unsigned long) * 8);unsigned long:bytes 4;bit 32取值范围:printf("unsigned long:MIN is 0;MAX %lun", ULONG_MAX);unsigned long:MIN is 0;MAX 4294967295 即0~2^32-1格式说明符:%lulong long(有符号长长整型)占用的字节数:printf("long long :bytes %d;bit %dn", sizeof(long long), sizeof(long long) * 8);long long :bytes 8;bit 64取值范围:printf("long long :MIN %lld;MAX %llun", LLONG_MIN, LLONG_MAX);long long :MIN -9223372036854775808;MAX 9223372036854775807 即-2^63~2^63-1格式说明符:%lluunsigned long long(有符号的长长整型)占用的字节数:printf("unsigned long long:bytes %d;bit %dn", sizeof(unsigned long long), sizeof(unsigned long long) * 8);unsigned long long:bytes 8;bit 64取值范围:printf("unsigned long long:MIN is 0;MAX %llun", ULLONG_MAX);unsigned long long:MIN is 0;MAX 18446744073709551615 即0~2^64-1格式说明符:%llufloat (单精度浮点型)占用的字节数:printf("float :bytes %d;bit %dn", sizeof(float), sizeof(float) * 8);float :bytes 4;bit 32取值范围:C标准规定,float类型至少能表示6位有效数字,范围至少为10^-37~10^37诸如:22.333444这样的数。其中8bit用于表示指数及其符号,24bit表示尾数或有效数字及其符号。 double(双精度浮点型)占用的字节数:printf("float :bytes %d;bit %dn", sizeof(float), sizeof(float) * 8);double :bytes 8;bit 64取值范围:C标准规定,double类型至少能表示10位有效数字。一些系统将多出来的32bit都来表示尾数部分,这就至少有13位有效数字了,这也超出了c规定的最小标准了。longdouble占用的字节数:printf("long double :bytes %d;bit %dn", sizeof(long double), sizeof(long double) * 8);long double :bytes 8;bit 64 取值范围:理论上是比double有更高的精度,但是c只保证long double至少同double 一样的精度 源码:include include int main() { printf("int:bytes %d;bit %dn", sizeof(int), sizeof(int) * 8); //int:bytes 4;bit 32 printf("int:MIN %d;MAX %dn", INT_MIN, INT_MAX); // int:MIN -2147483648;MAX 2147483647 printf("unsigned int:bytes %d;bit %dn", sizeof(unsigned int), sizeof(unsigned int) * 8); //unsigned int:bytes 4;bit 32 printf("unsigned int:MIN is 0;MAX %un", UINT_MAX); //unsigned int:MIN is 0;MAX 4294967295 printf("-------------------------------------------------------n"); printf("char:bytes %d;bit %dn", sizeof(char), sizeof(char) * 8); // char:bytes 1;bit 8 printf("char:MIN %d;MAX %dn", CHAR_MIN, CHAR_MAX); // char:MIN -128;MAX 127 printf("unsigned char:bytes %d;bit %dn", sizeof(unsigned char), sizeof(unsigned char) * 8); //unsigned char:bytes 1;bit 8 printf("unsigned char:MIN is 0;MAX %dn", UCHAR_MAX); // unsigned char:MIN is 0;MAX 255 printf("-------------------------------------------------------n"); printf("short:bytes %d;bit %dn", sizeof(short), sizeof(short) * 8); // short:bytes 2;bit 16 printf("short :MIN %hd;MAX %hdn", SHRT_MIN, SHRT_MAX); // short :MIN -32768;MAX 32767 printf("unsigned short:bytes %d;bit %dn", sizeof(unsigned short), sizeof(unsigned short) * 8); //unsigned short:bytes 2;bit 16 printf("unsigned short:MIN is 0;MAX %dn", USHRT_MAX); // unsigned short:MIN is 0;MAX 65535 printf("-------------------------------------------------------n"); printf("long:bytes %d;bit %dn", sizeof(long), sizeof(long) * 8); // long:bytes 4;bit 32 printf("long :MIN %ld;MAX %ldn", LONG_MIN, LONG_MAX); // long :MIN -2147483648;MAX 2147483647 printf("unsigned long:bytes %d;bit %dn", sizeof(unsigned long), sizeof(unsigned long) * 8); //unsigned long:bytes 4;bit 32 printf("unsigned long:MIN is 0;MAX %lun", ULONG_MAX); // unsigned long:MIN is 0;MAX 4294967295 printf("-------------------------------------------------------n"); printf("long long :bytes %d;bit %dn", sizeof(long long), sizeof(long long) * 8); // long long :bytes 8;bit 64 printf("long long :MIN %lld;MAX %llun", LLONG_MIN, LLONG_MAX); // long long :MIN -9223372036854775808;MAX 9223372036854775807 printf("unsigned long long:bytes %d;bit %dn", sizeof(unsigned long long), sizeof(unsigned long long) * 8); //unsigned long long:bytes 8;bit 64 printf("unsigned long long:MIN is 0;MAX %llun", ULLONG_MAX); // unsigned long long:MIN is 0;MAX 18446744073709551615 printf("-------------------------------------------------------n"); printf("float :bytes %d;bit %dn", sizeof(float), sizeof(float) * 8); // float :bytes 4;bit 32 printf("double :bytes %d;bit %dn", sizeof(double), sizeof(double) * 8); // double :bytes 8;bit 64 printf("long double :bytes %d;bit %dn", sizeof(long double), sizeof(long double) * 8); // long double :bytes 8;bit 64 printf("-------------------------------------------------------n"); return 0;  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-8 13:16 , Processed in 1.114969 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表