Skip to content

Instantly share code, notes, and snippets.

@0x5742
Created June 10, 2015 21:53
Show Gist options
  • Select an option

  • Save 0x5742/86bda4129461c4f74907 to your computer and use it in GitHub Desktop.

Select an option

Save 0x5742/86bda4129461c4f74907 to your computer and use it in GitHub Desktop.
Named parameters in C
/* 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");
}
@RepellantMold
Copy link
Copy Markdown

damn that's crazy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment