2013-02-28 59 views
-5

这是c的简单程序,我想知道在使用Dev-Cpp编译器运行时输出是如何为0的?解释给定代码的输出?

int main() { 
    printf ("%d\n",(float)((int)3.5/2)); 
    return 0; 
} 

回答

4

你传递一个float%d(你应该已经通过int%d) - 行为是不确定的,什么是允许的。

3

请标志-Wextra -Wall -Werror编译)

printf ("%d\n",(float)((int)3.5/2)); // error compile 

format ‘%d’ expects argument of type ‘int’, but argument 2 has type double 

测试:

printf ("%d\n",((int)3.5/2)); // 3.5/2 = 1.75 cast to int: output = 1; 


printf ("%f\n", 3.5/2); // 3.5/2 = 1.75 no cast: output = 1.75; 

看的人的printf

+0

'-W'现在是'-Wextra'。尽管'-W'仍然可以识别,但我建议使用新的表单。 – 2013-02-28 14:02:45

+0

谢谢;)@DanielFischer – 2013-02-28 14:04:28