Skip to content

Instantly share code, notes, and snippets.

@m1lkweed
Created July 26, 2022 05:20
Show Gist options
  • Save m1lkweed/da18ece5eb2ce940de86272b302600cb to your computer and use it in GitHub Desktop.
Save m1lkweed/da18ece5eb2ce940de86272b302600cb to your computer and use it in GitHub Desktop.
sizeof implemented as a macro
#define sizeof(x) ((size_t)(((typeof(x)*)0)+1))
#include <stdio.h>
struct foo{
char a,b,c,d,e;
};
int main(){
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(int*));
printf("%zu\n", sizeof(char));
printf("%zu\n", sizeof(char*));
printf("%zu\n", sizeof(&main));
printf("%zu\n", sizeof(int[3]));
printf("%zu\n", sizeof("Hello!"));
printf("%zu\n", sizeof(struct foo));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment