Skip to content

Instantly share code, notes, and snippets.

@hikari-no-yume
Last active August 29, 2015 14:27
Show Gist options
  • Save hikari-no-yume/7ba3142626f1c9ed4e97 to your computer and use it in GitHub Desktop.
Save hikari-no-yume/7ba3142626f1c9ed4e97 to your computer and use it in GitHub Desktop.
C's switch() with only macros | http://codepad.org/4z67amon
#include "stddef.h"
#include "stdio.h"
#include "stdbool.h"
#define BEGIN_SWITCH(expr) { int _val = expr; bool chosen = false; {
#define END_SWITCH() } } _switch_end: NULL;
#define CASE(val) } if (chosen || _val == val) { chosen = true;
#define BREAK() goto _switch_end;
int main () {
BEGIN_SWITCH(2)
CASE(1)
printf("hello, world\n");
CASE(2)
printf("hello, universe\n");
CASE(3)
printf("hello, fallthrough\n");
BREAK()
CASE(4)
printf("hello, nobody\n");
END_SWITCH()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment