如何为float的转换/双printf中为int处理? [英] How is conversion of float/double to int handled in printf?

查看:139
本文介绍了如何为float的转换/双printf中为int处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这一计划。

int main()
{
        float f = 11.22;
        double d = 44.55;
        int i,j;

        i = f;         //cast float to int
        j = d;         //cast double to int

        printf("i = %d, j = %d, f = %d, d = %d", i,j,f,d);
        //This prints the following:
        // i = 11, j = 44, f = -536870912, d = 1076261027

        return 0;
}

有人能解释为什么从double /浮到INT铸造正常工作在第一种情况,并在printf的?结果完成后不工作
这个程序被编译在​​GCC-4.1.2的32位Linux机器上。

Can someone explain why the casting from double/float to int works correctly in the first case, and does not work when done in printf?
This program was compiled on gcc-4.1.2 on 32-bit linux machine.

编辑:
<一href=\"http://stackoverflow.com/questions/2398791/how-is-conversion-of-float-double-to-int-handled-in-printf/2398821#2398821\">Zach's回答似乎是合乎逻辑的,即使用格式说明找出弹出堆栈。然而再考虑这个跟进的问题:

Zach's answer seems logical, i.e. use of format specifiers to figure out what to pop off the stack. However then consider this follow up question:

int main()
{

    char c = 'd';    // sizeof c is 1, however sizeof character literal
                     // 'd' is equal to sizeof(int) in ANSI C

    printf("lit = %c, lit = %d , c = %c, c = %d", 'd', 'd', c, c);
    //this prints: lit = d, lit = 100 , c = d, c = 100
    //how does printf here pop off the right number of bytes even when
    //the size represented by format specifiers doesn't actually match 
    //the size of the passed arguments(char(1 byte) & char_literal(4 bytes))    

 return 0;
}

这是如何工作的?

How does this work?

推荐答案

的printf 函数使用格式说明找出弹出堆栈。所以当它看到%d个,它会弹出4个字节,跨$ P $其中pts他们作为一个 INT ,其中是错误的(二进制重新$ p $ 的psentation(浮点)3.0 是不一样的(INT)3 )。

The printf function uses the format specifiers to figure out what to pop off the stack. So when it sees %d, it pops off 4 bytes and interprets them as an int, which is wrong (the binary representation of (float)3.0 is not the same as (int)3).

您需要的将是使用%F 格式说明或铸铁的参数 INT 。如果您使用 GCC 新版本不够,那么强烈的警告打开捕获这种错误的:

You'll need to either use the %f format specifiers or cast the arguments to int. If you're using a new enough version of gcc, then turning on stronger warnings catches this sort of error:

$ gcc -Wall -Werror test.c
cc1: warnings being treated as errors
test.c: In function ‘main’:
test.c:10: error: implicit declaration of function ‘printf’
test.c:10: error: incompatible implicit declaration of built-in function ‘printf’
test.c:10: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘double’
test.c:10: error: format ‘%d’ expects type ‘int’, but argument 5 has type ‘double’

回答这个问题的编辑部分:

Response to the edited part of the question:

C'S整数提升规则说,所有类型的比 INT 较小时得到一个可变参数传递晋升为 INT 。所以你的情况,在D是获得晋升到 INT ,然后printf的是突然离开的 INT 和铸造到字符。我能找到这种行为的最好的参考就是这篇博客

C's integer promotion rules say that all types smaller than int get promoted to int when passed as a vararg. So in your case, the 'd' is getting promoted to an int, then printf is popping off an int and casting to a char. The best reference I could find for this behavior was this blog entry.

这篇关于如何为float的转换/双printf中为int处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆