Following is a code to see how different data types are stored in memory.
#include <stdio.h>
void newline(void)
{
putchar('\n');
}
void showbyte(char *string, int len)
{
int i;
for (i = 0; i < len; i++)
printf("%p\t0x%.2x\n", string+i, *(string+i));
}
int main()
{
int i = 12345;
float f = 1234.5;
double d = 1234.5;
char name[] = "12345";
showbyte((char *)&i, sizeof i);
newline();
showbyte((char *)&f, sizeof f);
newline();
showbyte((char *)&d, sizeof d);
newline();
showbyte((char *)&name, sizeof name);
return 0;
}
Output
0x7fff8a9ab2cc 0x39
0x7fff8a9ab2cd 0x30
0x7fff8a9ab2ce 0x00
0x7fff8a9ab2cf 0x00
0x7fff8a9ab2c8 0x00
0x7fff8a9ab2c9 0x50
0x7fff8a9ab2ca 0xffffff9a
0x7fff8a9ab2cb 0x44
0x7fff8a9ab2c0 0x00
0x7fff8a9ab2c1 0x00
0x7fff8a9ab2c2 0x00
0x7fff8a9ab2c3 0x00
0x7fff8a9ab2c4 0x00
0x7fff8a9ab2c5 0x4a
0x7fff8a9ab2c6 0xffffff93
0x7fff8a9ab2c7 0x40
0x7fff8a9ab2b0 0x31
0x7fff8a9ab2b1 0x32
0x7fff8a9ab2b2 0x33
0x7fff8a9ab2b3 0x34
0x7fff8a9ab2b4 0x35
0x7fff8a9ab2b5 0x00
The IEEE-754 representation for float 1234.5
is 0x449a5000
, and for double 1234.5
is 0x40934A0000000000
. When it printed the float
and double
variable contents, it shows a 4-byte
content.
ie,
0x7fff8a9ab2ca 0xffffff9a
and
0x7fff8a9ab2c6 0xffffff93
.
But each memory location can store only 1-byte
of data, then why does it happen?