Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active December 18, 2021 05:58
Show Gist options
  • Select an option

  • Save opsJson/8e4637b2da58c9e22ccc1dd7b23d47d1 to your computer and use it in GitHub Desktop.

Select an option

Save opsJson/8e4637b2da58c9e22ccc1dd7b23d47d1 to your computer and use it in GitHub Desktop.
Python keyword arguments in C.
#include <stdio.h>
//create your arguments list.
struct args {
char *name;
int x;
int y;
int width;
int height;
};
//create a function that receives a pointer to your arguments list.
void foo(struct args *a) {
printf("%s %i %i %i %i\n", a->name, a->x, a->y, a->width, a->height);
}
//create a macro that converts foo(...) to foo(pointer of your arguments list)
#define foo(...) foo(&(struct args) {__VA_ARGS__})
int main() {
foo(.x = 10, .y = 20, .name = "Hello, world!\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment