访问说明符为浮动不符合预期 [英] Access specifier for float does not behave as expected

查看:101
本文介绍了访问说明符为浮动不符合预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< stdio.h> 
int main()
{
printf(%f,5);

$ / code>

此代码打印0.000000。任何人都可以解释为什么发生这种情况。

解决方案

这是因为在你的例子中, 5 是一个整数,而不是一个浮点数。因此,当你使用格式化字符串%f 将它传递给 printf 时,你会得到一个未定义的行为。为了使它成为一个浮点数,用浮点数( 5.0 )声明它。这会给你预期的输出。



例子:

 #包括< stdio.h> 
int main()
{
printf(%f,5.0);



$ h $输出:

$ pre> 5.000000


#include <stdio.h>
int main()
{
    printf("%f",5);
}

This code is printing 0.000000. Can anyone explain why this is happening.

解决方案

This is because in your example, 5 is an integer, not a float. Thus, when you pass it to printf with the formatting string of %f, you get undefined behavior. To make it a float, declare it with the floating point (5.0). This will give you your expected output.

Example:

#include <stdio.h>
int main()
{
    printf("%f", 5.0);
}

Outputs:

5.000000

这篇关于访问说明符为浮动不符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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