Difference between float and double ?
As we know both float and double are used for storing decimal point numbers, and the size of both varies from compiler to compiler.
In general float occupies 4 bytes of memory and double occupies 8 bytes of memory.
Example:
#includeint main() { float x=0.5; double y=0.5; printf("size of x=%d\nsize of y=%d",sizeof(x),sizeof(y)); return 0; } Output: size of x=4 size of y=8
But see the next program,
#includeint main() { float x=0.5; double y=0.5; printf("size of x=%d\nsize of y=%d\nsize of variable=%d",sizeof(x),sizeof(y),sizeof(0.5)); return 0; }
Output:
size of x=4 size of y=8 size of variable=8
Have you observed the fact ??
Now, observe one more point…
#includeint main() { float x=0.5; double y=0.5; printf("size of x=%d\nsize of y=%d\nsize of variable=%d\nsize of second variable=%d",sizeof(x),sizeof(y),sizeof(0.5),sizeof(0.5f)); return 0; }
Output:
size of x=4 size of y=8 size of variable=8 size of second varaible=4
So, there is a big difference in 0.5 and 0.5f,
0.5 , is by default double but when we assign it in any float variable.