-
Junior Member
What is value of printf("%d",12.5);
Dear friends,
I am facing a problem in my small code. I was just wondering that what is the output of the code and why did it happen.
The code is
#include<stdio.h>
main()
{
float i=12.5;
printf("%d",i);
getchar();
}
It gives me 0 as output.
But i am confused why is it so.
Please help me in finding out the reason behind it.
-
Junior Member
Re: What is value of printf("%d",12.5);
Its giving as 0 that is due to the change in formats specified in both declaration and printf statements...
It is to be noted if u declare an integer variable and in printf if u give %f then also a garbagge value is output.
-
Junior Member
Re: What is value of printf("%d",12.5);
but can I know what exactly happened in the storage of floats? how are floats stored in memory?
-
Junior Member
Re: What is value of printf("%d",12.5);
actually when u give
float i=12.5;
4 byte of memory is allocated for the float variable.But when u give
printf("%d",i); it accesses the first 2 bytes of the float variable (as it is now asked to o/p the integer i .Hence you get to see a 0 not 12.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules