Created
June 10, 2015 21:53
-
-
Save 0x5742/86bda4129461c4f74907 to your computer and use it in GitHub Desktop.
Named parameters in C
This file contains hidden or 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
| /* This is a way to make a function with named parameters in standard C99. | |
| Most typical programmer errors (parameter typo, etc.) produce at least | |
| somewhat reasonable compiler messages. | |
| Unlike some other methods of doing this, this supports default parameters | |
| which can be defined to any value, and does not require any cumbersome | |
| syntax such as casts or struct accessors when calling the function. | |
| Originally posted on 4chan /prog/. | |
| -- Storlek */ | |
| #include <stdio.h> | |
| int __madlib(int r, char *music, char *language, char *adjective) | |
| { | |
| printf("I listen to %s while coding in %s because I am %s\n", | |
| music, language, adjective); | |
| return r; | |
| } | |
| #define madlib(r, ...) ({ \ | |
| char *music = "Infected Mushroom", *language = "Java", *adjective = "1337"; \ | |
| __VA_ARGS__; __madlib(r, music, language, adjective); }) | |
| int main(int argc, char **argv) | |
| { | |
| return madlib(0, language = "C"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
damn that's crazy