7

我正在为 Little Endian 使用 Visual Studio TC 编译器。以下是一段代码:

void main()
{    
    float c = 1.0;
    int a = 0x3F800000;
    int *ptr = (int *)&c;
    printf("\n0x%X\n", *ptr);
    printf("\na = %f", a);
    printf("\nc = %f", c);
    return;    
}

输出是:

0x3F800000
a = 0.000000
c = 1.000000

浮点值 1.0 为 0x3F800000 并以 00 00 80 3F 的形式存储在 Little Endian 的内存中。将相同的值分配给 int a。printf 如何为 int 打印 0.000000 而为 float c 打印 1.000000?我已经看到它在 printf 中使用 %f 打印时将所有整数值打印为 0.000000。

另外,由于 printf 是可变参数函数,它如何知道寄存器中传递的值是 int 还是 float?

4

7 回答 7

6

My psychic powers tell me Adam Liss's comment is the right answer: float arguments are promoted to double, so the printf() function expects that to happen: It expects a 64-bit value on the stack, but gets 32 bits plus garbage data that happen to be zero.

If you increase the display precision, the display should be something like a = 0.00000000001.

This also means this should work:

void main()
{    
    double c = 1.0;
    long long a = 0x3FF0000000000000;
    long long *ptr = (long long *)&c;
    printf("\n0x%llX\n", *ptr);
    printf("\na = %f", a);
    printf("\nc = %f", c);
    return;    
}
于 2013-05-17T11:46:38.463 回答
5

我已经在 gcc 中编译了您的代码,生成的代码如下:

movl    $0x3f800000, %eax
movl    %eax, -4(%ebp)
movl    $1065353216, -8(%ebp)
leal    -4(%ebp), %eax
movl    %eax, -12(%ebp)
movl    -12(%ebp), %eax
movl    (%eax), %eax
movl    %eax, 4(%esp)
movl    $LC1, (%esp)
call    _printf
movl    -8(%ebp), %eax
movl    %eax, 4(%esp)
movl    $LC2, (%esp)
call    _printf
flds    -4(%ebp)
fstpl   4(%esp)
movl    $LC3, (%esp)
call    _printf

这可以给你一个提示,浮点参数不是从常规堆栈中获取的,而是从浮点堆栈中获取的……我希望会有一些随机的东西而不是 0……

于 2013-05-17T11:27:19.530 回答
5
于 2013-05-17T11:42:33.267 回答
2

Within any C implementation, there are rules about how parameters are passed to functions. These rules may say that parameters of certain types are passed in certain registers (e.g., integer types in general registers and floating-point types in separate floating-point registers), that large arguments (such as structures with many elements) are passed on the stack or by a pointer to a copy of the structure, and so on.

Inside a called function, the function looks for the parameter it expects in the places that the rules specify. When you pass an integer in an argument to printf but pass it %f in the format string, you are putting an integer somewhere but telling printf to look for a float (that has been promoted to a double). If the rules for your C implementation specify that the integer argument is passed in the same place as a double argument, then printf will find the bits of your integer, but it will interpret them as a double. On the other hand, if the rules for your C implementation specify different places for the arguments, then the bits of your integer are not where printf looks for the double. So printf finds some other bits that have nothing to do with your integer.

Also, many C implementations have 32-bit int types and 64-bit double types. The %f specifier is for printing a double, not a float, and the float value you pass is converted to double before calling the function. So, even if printf finds the bits of your integer, there are only 32 bits there, but printf uses 64. So the double that is printed is made up of 32 bits you passed and 32 other bits, and it is not the value you intended to print.

This is why the format specifiers you use must match the arguments you pass.

于 2013-05-17T12:32:46.207 回答
1

I meet the similar issue , and finally I develop a way to solve it , not sure whether that's what you want.The key point is that : you should pass a float instead of a integer .

#include <stdio.h>
void printIntAsFloat();

int main(void){
    printIntAsFloat();
}

void printIntAsFloat(){
    float c = 1.0;
    int a = 0x3F800000;
    int *ptr = (int *)&c;
    float *fp = (float*)((void*)&a); /*this is the key point*/
    printf("\n0x%X\n", *ptr);
    printf("\na = %f", a);
    printf("\nc = %f", c);
    printf("\nfp = %f", *fp);
    return; 
} 

the output is like this :

0x3F800000

a = 0.000000
c = 1.000000
fp = 1.000000

OS : Fedora21 64 bit GCC version :gcc version 4.9.2 20141101 (Red Hat 4.9.2-1) (GCC)

于 2015-04-07T01:23:14.103 回答
-1

转换变量

printf("\na = %f", (float)a);
printf("\nc = %f", (float)c);
于 2013-05-17T11:26:57.197 回答
-1
int a= 0x3F800000;
printf("\na = %f", *(float*)&a);//1.0
于 2013-05-17T11:43:02.883 回答