Last active
August 29, 2015 14:27
-
-
Save hikari-no-yume/7ba3142626f1c9ed4e97 to your computer and use it in GitHub Desktop.
C's switch() with only macros | http://codepad.org/4z67amon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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