|
1 2 3 4 5 6 7 8 9 |
#include <cstdio> using namespace std; int main() { int a = 3; printf("int a print float : %f", a); double b = 3.4; printf("\ndouble b print int : %d", b); return 0; } |
输出为:
|
1 2 |
int a print float : 0.000000 double b print int : 73832 |
- int输出%f浮点值的时候,比如2,2内部表示如果看作是float,是个很小的数所以输出的是0.000000
- double/float 浮点数按照%d输出,如果输出为0,则说明a的数据放在地址的高端,而整型比浮点数内存中占的字节数少,整型只会把属于它的字节数读出来,如在Win32,VC6.0下,Int是4位,它就会把从a开始的4位读出来(按整型格式),所以它把浮点数低端地址的0给输出出来。
- 所以说使用printf的时候数据格式一定要对应,或者使用
printf("%d", (int)a);这样强制转换的方法输出不同格式的数据