为什么我的C代码给出0作为输出? [英] Why my C code giving 0 as output?

查看:89
本文介绍了为什么我的C代码给出0作为输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用浮点变量f并用3.3初始化它。后来我尝试使用%d格式说明符打印输出,但输出为0。现在我的问题是为什么我得到0而不是3?



我尝试过的事情:



void main()

{

float f = 3.3;

printf(f的值是:%d ,f);

gecth();

}

解决方案

因为你试图打印一个将值浮动为整数。 (你得到的实际结果将取决于系统,你也不能依赖它为零 - 我的编译器给我12!)

尝试:

< pre lang =c ++> printf( f的值为:%f \ n, f);

 printf(  f的值是:%d \ n,( int )f); 


I use a float variable f and initialize it with 3.3. Later I tried to print output using %d format specifier but I got 0 as output. Now my question is why I got 0 but not 3?

What I have tried:

void main()
{
float f=3.3;
printf("value of f is : %d",f);
gecth();
}

解决方案

Because you are trying to print a float value as an integer. (The actual result you get will be system dependant, you can't rely on it being zero either - my compiler gives me "12"!)
Try:

printf("value of f is : %f\n",f);

Or

printf("value of f is : %d\n",(int)f);


这篇关于为什么我的C代码给出0作为输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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