How will implement Your Own sizeof in C


To use the sizeof(), we can take the value using a variable x, using &x, it will print the address of it. Now if we increase the value of &x then it may increase in different way. If only one byte is increased, that means it is character, if the increased value is 4, then it is int or float and so on. So by taking the difference between &x + 1 and &x, we can get the size of x.

Here we will use macro as the datatype is not defined in the function. And one more thing, we are casting using (char*) so, it will tell us that how many character type data can be placed in that place. As the character takes one byte of data.

Example

#include <stdio.h>
#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
main(void) {
   int x = 10;
   char y = 'f';
   double z = 254748.23;
   printf("size of x: %d
", my_sizeof(x));    printf("size of y: %d
", my_sizeof(y));    printf("size of z: %d
", my_sizeof(z)); }

Output

size of x: 4
size of y: 1
size of z: 8

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements