Skip to content

Instantly share code, notes, and snippets.

@nvictor
Last active February 20, 2020 23:51
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 nvictor/8b9079f8c59e7eb980894f76cf9b1e26 to your computer and use it in GitHub Desktop.
Save nvictor/8b9079f8c59e7eb980894f76cf9b1e26 to your computer and use it in GitHub Desktop.
C Macros
#define LEN(A) (sizeof(A) / sizeof((A)[0]))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
// MAX3, MAX4, etc could be implented in terms of MAX, i.e. MAX((a), MAX((b), (c)))
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
// C static assert
// if expr is true then evaluates sizeof(char[1])
// if expr is false then evaluates sizeof(char[-1]) which is undefined behavior
#define SASSERT(expr) ((void)sizeof(char[1 - 2 * !(expr)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment