Sunday, January 13, 2013

sizeof

I just learned how to use sizeof. From http://msdn.microsoft.com/en-us/library/4s7x1k91.aspx, sizeof will return the size of the total number of byte of variable or pointer.

For example:
int has 4 bytes.
int x;
printf("%d",sizeof(x)); -> its output will be 4

int x[5];
printf("%d",sizeof(x)); -> its output will be 20 -> 5*4

However, if I use pointer, it will return the size of the pointer.
int *x;
char *y;
printf("%d",sizeof(x)); -> its output will be 4
printf("%d",sizeof(y)); -> its output will be 4
printf("%d",sizeof(*y)); -> its output will be 1

you can check the size of the type by doing
printf("%d\n",type);

No comments:

Post a Comment