Created
December 1, 2023 23:29
-
-
Save fdmysterious/1a1b4aa0b000d53d109c2b29a2e31b44 to your computer and use it in GitHub Desktop.
Named argument in C99
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
/** | |
* \brief Named arguments in C99 | |
* \author Florian Dupeyron <[email protected]> | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
struct FooArgs { | |
int a; | |
int b; | |
int c; | |
}; | |
void foo_impl(struct FooArgs args) { | |
printf("Calling foo with a = %d, b = %d, c = %d\n", args.a, args.b, args.c); | |
} | |
#define foo(...) foo_impl((struct FooArgs){ __VA_ARGS__}) | |
int main(int argc, char* argv[]) { | |
// Calling with struct init | |
foo_impl((struct FooArgs){ | |
.a = 4, | |
.b = 12, | |
.c = 36 | |
}); | |
// Calling with macro | |
foo( | |
.a = 5, | |
.b = 13, | |
.c = 37 | |
); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment