Sunday, November 1, 2015

custom sizeof implementation

#include <stdio.h>

#define my_sizeof(type) (((type *)0)+1)

int main()
{
    printf("size of int = %d\n", sizeof(int));
    printf("My size of int = %d\n", my_sizeof(int));
    printf("My size of char = %d\n", my_sizeof(char));
    printf("My size of float = %d\n", my_sizeof(float));
    printf("My size of double = %d\n", my_sizeof(double));
    return 0;
}


Result:
   size of int = 4
   My size of int = 4
   My size of char = 1
   My size of float = 4
   My size of double = 8

No comments: