Skip to content

Instantly share code, notes, and snippets.

@x100ex
Created June 17, 2017 09:39
Show Gist options
  • Save x100ex/f61d583b4d1c93868da24fa4d47bb262 to your computer and use it in GitHub Desktop.
Save x100ex/f61d583b4d1c93868da24fa4d47bb262 to your computer and use it in GitHub Desktop.
static assert in C
// C compiler check for structure size
// from: http://bytes.com/topic/c/answers/220022-if-sizeof
#define _x_CCASERT_LINE_CAT(predicate, line) typedef char constraint_violated_on_line_##line[2*((predicate) != 0)-1];
#define CCASSERT(predicate) _x_CCASERT_LINE_CAT(predicate, __LINE__)
// Usage: CCASSERT(1) to pass; CCASSERT(0) to fail
/*
typedef struct {
long x;
long y;
}foo ;
CCASSERT(sizeof(foo) < 10) // will not complain
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment