Skip to content

Instantly share code, notes, and snippets.

@aprell
Created September 30, 2012 17:56
Show Gist options
  • Save aprell/3807945 to your computer and use it in GitHub Desktop.
Save aprell/3807945 to your computer and use it in GitHub Desktop.
Named parameters and default argument values in C
#include <stdio.h>
#include <stdlib.h>
struct rectangle {
double x1, x2, y1, y2;
};
double area(struct rectangle r)
{
return (r.x2 - r.x1) * (r.y2 - r.y1);
}
#define area(...) area((struct rectangle) \
{ .x1 = 1.0, .x2 = 4.0, .y1 = 1.0, .y2 = 5.0, __VA_ARGS__})
int main(void)
{
printf("%g\n", area());
printf("%g\n", area(.x1 = 0.0));
printf("%g\n", area(.x2 = 2.5, .y2 = 10.5));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment