Skip to content

Instantly share code, notes, and snippets.

@Bktero
Last active March 23, 2024 23:45
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Bktero/3fb41a617588b914317af6d678e08c54 to your computer and use it in GitHub Desktop.
Save Bktero/3fb41a617588b914317af6d678e08c54 to your computer and use it in GitHub Desktop.
[C][C++] Print size of type at compile time
char (*__kaboom)[sizeof( xxx )] = 1;
// xxx can be a variable or a data type
// See https://stackoverflow.com/questions/20979565/how-can-i-print-the-result-of-sizeof-at-compile-time-in-c
// Exemple of code:
// char (*__kaboom)[sizeof( long long )] = 1;
// GCC error:
// error: invalid conversion from 'int' to 'char (*)[8]' [-fpermissive]
// So sizeof(long long) == 8
@Anonymous-Ol
Copy link

Works great. Surprised the C standard people haven't added a way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment