Write your own sizeof function in C

This is the most common interview asked , i have seen qualcomm asking this question in their first round.

sizeof is used to calculate the size of any datatype.It is implemented by compilers.

We will write a macro that performs the same functionality as by the sizeof implemented by compiler.

#include 

#define SIZEOF(X) (int)(((typeof(X)*)0)+1)

int main(int argc,char *argv[])
{

 int a;
 char b;
 float c;
 double d;
 int *e;
 int f[10];

 printf("size of a is %d\n",SIZEOF(a));
 printf("size of b is %d\n",SIZEOF(b));
 printf("size of c is %d\n",SIZEOF(c));
 printf("size of d is %d\n",SIZEOF(d));
 printf("size of e is %d\n",SIZEOF(e));
 printf("size of f is %d\n",SIZEOF(f));
 printf("size of char is %d\n",SIZEOF(char));

}


Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig