3 ms·
The combination of compound literals and variadic macros is indeed pretty neat. It can also be used to implement Java-style variadic functions (ie when the vari
by cygx 15y ago
The combination of compound literals and variadic macros is indeed pretty neat. It can also be used to implement Java-style variadic functions (ie when the variadic parameters have a single type):
#define sum(...) \
sum_(sizeof ((int []){ __VA_ARGS__ }) / sizeof (int), (int []){ __VA_ARGS__ })
int sum_(size_t count, int values[])
{
int s = 0;
while(count--) s += values[count];
return s;
}
You'd call the macro as
int s = sum(1, -32, 5);
- LefterisJP 15y agonice idea!