Skip to content

Instantly share code, notes, and snippets.

@nyrahul
Created March 13, 2021 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyrahul/d3df672c7dd7153da69d7fe35d1af1d6 to your computer and use it in GitHub Desktop.
Save nyrahul/d3df672c7dd7153da69d7fe35d1af1d6 to your computer and use it in GitHub Desktop.
Compile time check to validate structure size
#include <stdio.h>
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
int main(void)
{
struct t {
int x;
int y;
int z;
};
BUILD_BUG_ON(sizeof(struct t) < 10); // This passes compilation
BUILD_BUG_ON(sizeof(struct t) < 20); // This fails compilation
printf("ok\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment