#include<stdio.h>
int main(){
float a=2.5;
printf("%f\n",a);
printf(" as int = %d \n as float = %f \n as hex = %x",a,a,a);
printf("\n%f",a);
return 0;
}
The output is
2.500000
as int = 0
as float = 0.000000
as hex = 40040000
2.500000
So what is up with the middle 3 lines??? I am not sure about the hex representation but I am sure it shouldn't show 0 for the int and float representations.
I used codeblocks 8.2 for compiling and it uses gcc I think.
---------- Post added at 02:19 ---------- Previous post was at 02:04 ----------
Also,
#include<stdio.h>
int main(){
float a=2.5;
printf("%f\n",a);
printf(" as int = %d\n", a);
printf(" as float = %f\n",a);
printf(" as hex = %u\n", a);
printf("\n%f",a);
return 0;
}
here as int = 0, as float = 2.500000 and as hex = 0
But why??
I thought it should show as int = 2
and how do I see what hex value it keeps in memory for floating points?
In the line
printf(" as int = %d\n", (int)a);
is it necessary to explicitly type cast the float variable a?
I didn't do it, so ss that the reason why my code outputs 0?
And in
printf(" as hex = %x\n", (long double)a);
why did you type cast to long double, and I see it outputs 0 as well. Why is that?
Also,
printf("a as hex =%x\n",*p);
Can you give me a brief introduction to why this works, that is, why does it show the hex value?
Does it have anything to do with the fact that %x, %d etc and the data type long pertains to whole numbers while the data type float pertains to fractions?
Yes you need to or else how to tell the compiler that it is going to print a integer.( type demotion loose precession)
printf("a as hex =%x\n",*p);
Can you give me a brief introduction to why this works, that is, why does it show the hex value?
Does it have anything to do with the fact that %x, %d etc and the data type long pertains to whole numbers while the data type float pertains to fractions?
Why this works ??? Well you asked how it is put in the memory, Right??? So if it is in memory then it must be accessed through pointer. is not it?
So I choose a pointer of same size and assign the address to the pointer.
Now I need to print the value as a normal number in HEX i choose to print it as Unsigned Long.
But it is a hackish thing. it might not give result properly on all platform.
it is a program to display in the lcd.. it may be written with some logic of the programmer. but in a simple way volt[0] [1][2] as used to convert adv value into unit , tens , hundred places of the value to display 3 digit value of adc....