+ 2
If we use %d in printf to print a float variable than what it print in c ?
main() { float a=3.8; printf("%d",a); }
8 ответов
+ 3
It's because the binary representations of the floating point number and it's corresponding integer (when properly cast) are different.
+ 3
Don't use a format string in printf to cast one type to another unless their internal binary representations are the same.
+ 1
But it gives 0
+ 1
Strange... It gives 1342327848 without <stdio.h> and -773259752 with <stdio.h> in SoloLearn C Playground...
So, the compiler does not convert, it just views data as int and not float.
+ 1
%d is double precise float therefore won't work because you data type is a float it's recommended to use the%f (float) data type, so it will go like this 👇👇
#include <studio.h>
int main()
{
float a=3.8;
printf("%f",a);
}
Try it
+ 1
get my bad must have been a confusion thanks for clarifying
0
Rick Sanchez (C-137), "%d" format specifier is for integer (<int>). Double-precise float (<double>)'s format specifier is "%lf" (first letter is small L).
https://www.sololearn.com/learn/C/2914/ page 3