Ram*_*Ram 1 c format-specifiers
May be this is a simple question but I am not sure about how float variables are stored in memory and why it is behaving in this way, can someone please explain about the following behavior.
#include<stdio.h>
int main ()
{
int a = 9/5;
printf("%f\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
0.000000
Run Code Online (Sandbox Code Playgroud)
I have looked at some information on how float variables are stored in memory, it has stuff about mantissa, exponent and sign. But I am not getting how to relate that here.
int a = 9/5;
Run Code Online (Sandbox Code Playgroud)
执行整数除法并忽略余数,因此a设置为1.尝试使用%f给出未定义的行为来打印它,但是你偶然得到0.000000了它.
做
double a = 9./5.;
Run Code Online (Sandbox Code Playgroud)
相反,或者%d如果整数除法是期望的行为则打印.(float也会起作用,但a会double在传递时被提升printf,所以没有理由不使用double.)