In "printf", if i use "%f" it was working. But when i used "%d" it was not working. Why? I'm so beginner . Don't mind ! -_- | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In "printf", if i use "%f" it was working. But when i used "%d" it was not working. Why? I'm so beginner . Don't mind ! -_-

include <stdio.h> int main() { int a,b; float c; scanf("%d %d %f",&a,&b,&c); printf ("%d",a+b+c); return 0; }

29th Sep 2018, 7:44 AM
Anin Hasan
Anin Hasan - avatar
2 Answers
+ 3
If you want to print it as integer, you need explicit type cast: printf("%d", (int) (a+b+c));
29th Sep 2018, 9:10 AM
Matthias
Matthias - avatar
+ 2
So, basically, C automatically converts your values to a float type. Because of the difference in their structure (representation in memory) you need to specify the right type in your template string (%f - for float in your case)
29th Sep 2018, 8:35 AM
WittyBit
WittyBit - avatar