在测试printf
函数输出结果时,写了以下代码:html
/** * printf:格式化输出函数 * printf函数不会按照格式控制而对数据类型进行转换,无论三七二十一, * 抓到二进制数据就按照格式控制符对数据进行解析。 */ #include <stdio.h> int main(void) { //test_1 float a = 10.9; printf("%d\n", a); //以十进制形式输出带符号整数(正数不输出符号) //输出 -1073741824 //(printf 不会将浮点型变量 a 数据类型转换为整型,而是将按照浮点型格式存储的数据直接按照整型数据的格式进行解析打印) //test_2 int b = 10; printf("%f\n", b); //以小数形式输出单、双精度浮点型 //输出 0.000000 //test_3(数据类型转换) int num_i = 10; float num_f = (float)num_i; printf("%f\n", num_f); //输出 10.000000 //直接对变量进行类型强制转换时: //编译器先将内存中 num_i 的值当作整型数来处理,而后再按照浮点数的格式将其存储在内存中,最后 printf 以 %f (实数)格式打印出来 //test_4(与 test_一、test_二、test_3 进行对比) int* fa = (int*)&a; float* fb = (float*)&b; printf("%d\n", *fa); //输出结果 1093559910 printf("%d\n", *fb); //输出结果 0 getchar(); return 0; }
原本觉得test_4
的结果会和test_1
和test_2
结果相同,然而结果输出以下:函数
编译环境为 Dev_C++ 5.11,编译日志以下:工具
主要观察a
和*fa
输出结果:在 test_4 中,取变量 a 的地址,而后将其地址强制转换成整型数地址,再取其地址中的内容 printf 按照%d
格式输出。在这个过程当中并无改变地址内保存的内容,test_1 和 test_4 都是将其地址中保存的内容按照整型数据进行解析并打印输出,为何结果会不同呢??测试
主要参考了下面的几个帖子:.net
其中的重点是:printf 格式化输出,在传递参数时,若实参为 float 型数据,编译时会自动转换为 double 类型。我又写了一些代码用来测试,并总结这个问题,具体过程以下:3d
#include <stdio.h> void main() { //实型常量默认为 double 型,后面加 f/F 认为是 float 型 printf("%d\n", sizeof(10.9)); //输出结果 8 printf("%d\n", sizeof(10.9f)); //输出结果 4 }
#include <stdio.h> int main(void) { float a = 10.9; float b = 10.9f; double c = 10.9; double d = 10.9f; printf("%d\n", a); //输出结果 -1073741824 printf("%d\n", b); //输出结果 -1073741824 printf("%d\n", c); //输出结果 -858993459 printf("%d\n", d); //输出结果 -1073741824 printf("%d\n", 10.9); //输出结果 -858993459 printf("%d\n", 10.9f); //输出结果 -1073741824 printf("%d\n", (double)10.9f); //输出结果 -1073741824 //对比 test_4 int* fa = (int*)&a; printf("%d\n", *fa); //输出结果 1093559910 return 0; }
printf("%d\n", a); //输出结果 -1073741824 printf("%d\n", b); //输出结果 -1073741824 printf("%d\n", d); //输出结果 -1073741824 printf("%d\n", 10.9f); //输出结果 -1073741824 printf("%d\n", (double)10.9f); //输出结果 -1073741824
这里输出的都是-1073741824
,只看前四行,对于 printf 传递的实参都是 float 型(因为 printf 格式化输出,在传递参数时,若实参为 float 型数据,编译时都会自动转换为 double 类型数据),因此实际输出结果均等价于第五行,先将 float 型的数据,自动转换为 double 类型,再将 8 字节的 double 类型数据按照%d
格式解析输出。调试
printf("%d\n", c); //输出结果 -858993459 printf("%d\n", 10.9); //输出结果 -858993459
这里输出的都是-858993459
,对于 printf 传递的实参都是 double 型,因此直接将 8 字节的 double 类型数据按照%d
格式解析输出。那么这里的输出结果和上面的输出结果为何不一样??因为 float 类型和 double 类型直接转换会涉及到精度问题(看下面的代码),因此上面 float 类型的 10.9 转换为 double 类型的数据和 double 类型的 10.9 在计算机中是不一样的,因此按照整型数来对数据(01011···)解析,输出的结果是不一样的。日志
#include <stdio.h> int main() { printf("%.15f\n", 3.14f); printf("%.15f\n", (double)3.14f); printf("%.15f\n", 3.14); return 0; }
其输出的结果以下:code
int* fa = (int*)&a; printf("%d\n", *fa); //输出结果 1093559910
这里输出的结果是1093559910
,和上面的两种结果都不相同,是由于将 float 类型的变量 a 的地址强制转换成整型数地址后,*fa
将被当作是整型数(地址中保存的内容实际并无改变),因此将*fa
传递给 printf 函数,这里printf("%d\n", *fa);
只是将变量 a 地址中 4 个字节的数据内容按照%d
的格式进行解析并输出打印出来,所以,输出结果与上面两种结果都不一样。htm
后来我换了VS 2015
对程序进行编译,会直接给出警告,经过看VS 2015
编译器给出的警告或许就能够直接发现问题了。对于上面 2.打印以下数据 中的程序,VS 2015
的编译信息以下(结合行号看警告信息,注意第18行没有警告):
原本的程序只是为了测试 printf 函数的输出结果,用来验证“ printf 函数不会按照格式控制符而对数据类型进行强制转换。对于 printf,无论三七二十一,只是抓到二进制数据就按照格式控制符对数据进行解析。”可是细心观察,深刻思考,就会发现新大陆:)
补充(两天后)
能够打印出变量 a、b、c、d 的地址,而后用调试工具查看数据在计算机内部的存储,能够对上述的 总结 加以验证,请参考 评论 ,具体代码以下:
#include <stdio.h> int main(void) { float a = 10.9; float b = 10.9f; double c = 10.9; double d = 10.9f; //打印变量的地址,查看数据在计算机内部的存储 printf("%p\n", &a); // 0x412e6666 float 类型内部存储 printf("%p\n", &b); // 0x412e6666 float 类型内部存储 printf("%p\n", &c); // 0x4025cccccccccccd double类型内部存储 printf("%p\n", &d); // 0x4025ccccc0000000 由float类型转换为double类型,其内部存储 printf("\n****************\n"); //如下的printf输出语句是等价的 printf("%d\n", a); //输出结果 -1073741824 printf("%d\n", b); //输出结果 -1073741824 printf("%d\n", d); //输出结果 -1073741824 printf("%d\n", 10.9f); //输出结果 -1073741824 printf("%d\n", (double)a); //输出结果 -1073741824 printf("%d\n", (double)b); //输出结果 -1073741824 printf("%d\n", (double)10.9f); //输出结果 -1073741824 printf("%d\n", 0x4025ccccc0000000); //输出结果 -1073741824 //printf对数据解析:截取低位四个字节 0xc0000000 //补码:1100 0000 0000 0000 0000 0000 0000 0000 //求原码,符号位不变,其他位取反加 1 //原码:1100 0000 0000 0000 0000 0000 0000 0000 //十进制表示:-1073741824 printf("\n****************\n"); //如下的printf输出语句是等价的 printf("%d\n", c); //输出结果 -858993459 printf("%d\n", 10.9); //输出结果 -858993459 printf("%d\n", 0x4025cccccccccccd); //输出结果 -858993459 //printf对数据解析:截取低位四个字节 0xcccccccd //补码:1100 1100 1100 1100 1100 1100 1100 1101 //求原码,符号位不变,其他位取反加 1 //原码:1011 0011 0011 0011 0011 0011 0011 0011 //十进制表示:-858993459 printf("\n****************\n"); //对比 test_4 int* fa = (int*)&a; //如下的printf输出语句是等价的 printf("%d\n", *fa); //输出结果 1093559910 printf("%d\n", 0x412e6666); //输出结果 1093559910 //printf对数据解析:0x412e6666 //补码:0100 0001 0010 1110 0110 0110 0110 0110 //求原码,正数的原码、反码和补码相同 //原码:0100 0001 0010 1110 0110 0110 0110 0110 //十进制表示:1093559910 getchar(); return 0; }
具体输出结果以下所示: