printf 为浮点数指定整数格式字符串 [英] printf specify integer format string for float

查看:20
本文介绍了printf 为浮点数指定整数格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include int main(){浮动 a = 5;printf("%d", a);返回0;}

这给出了输出:

<预><代码>0

为什么输出为零?

解决方案

%d 格式说明符只能与 int 类型的值一起使用.您正在传递一个 double (其中 float 将被隐式转换为).结果行为是未定义的.为什么打印 0?"没有答案.问题.什么都可以打印.事实上,任何事情都可能发生.

附言

  1. 那是int main,而不是void main.
  2. 标准 C 中没有 conio.h 这样的头文件.

#include <stdio.h>

int main()
{
    float a = 5;
    printf("%d", a);
    return 0;
}

This gives the output:

0

Why is the output zero?

解决方案

%d format specifier can only be used with values of type int. You are passing a double (which float will be implicitly converted to). The resultant behavior is undefined. There no answer to "why it prints 0?" question. Anything can be printed. In fact, anything can happen.

P.S.

  1. That's int main, not void main.
  2. There's no such header as conio.h in standard C.

这篇关于printf 为浮点数指定整数格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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