使用%F打印一个整型变量 [英] Using %f to print an integer variable

查看:228
本文介绍了使用%F打印一个整型变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的C程序的输出是:
0.000000
是否有输出的背后逻辑,或者答案依赖编译器或我刚开垃圾的价值?

The output of the following c program is: 0.000000 Is there a logic behind the output or is the answer compiler dependent or I am just getting a garbage value?

#include<stdio.h>

int main()
{
    int x=10;
    printf("%f", x);
    return 0;
}

PS: - 我知道,尝试打印使用%f是愚蠢的整数值。我只是从理论角度问这个。

PS:- I know that to try to print an integer value using %f is stupid. I am just asking this from a theoretical point of view.

推荐答案

从最新的 C11 草案:

§7.16.1.1/ 2

...if type is not compatible with the type of the actual next argument 
(as promoted according to the default argument promotions), the behavior 
is undefined, except for the following cases:

— one type is a signed integer type, the other type is the corresponding 
unsigned integer type, and the value is representable in both types;
— one type is pointer to void and the other is a pointer to a character type.

这篇关于使用%F打印一个整型变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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