Manage arguments of macros in C.
Manage arguments of macros in C.
Download: header file.
// Get nth argument.
EMA_GET<N>(args)
// get index 0 (first) argument
printf("EMA_GET0(a0, a1, a2): %s.\n", EMA_GET0("a0", "a1", "a2"));
// get max of 3 values using variable argument (variadic) macro
#define MAX2(A, B) ((a)>(b)? (a) : (b))
#define MAX3(A, B, C) MAX2(MAX2(A, B), C)
#define MAX(...) EMA_GET0(EMA_GET3(__VA_ARGS__, MAX3, MAX2)(__VA_ARGS__))
printf("MAX(7, 1, 2): %d.\n", MAX(7, 1, 2));
// Count number of arguments.
EMA_COUNT(args)
printf("EMA_COUNT(3, 5, 2, 3): %d.\n", EMA_COUNT(3, 5, 2, 3));
// Get even index arguments.
EMA_GETEVEN(args)
printf("EMA_GETEVEN(7, 1, 2, 3): %d, %d.\n", EMA_GETEVEN(7, 1, 2, 3));
// Get odd index arguments.
EMA_GETODD(args)
printf("EMA_GETODD(7, 1, 2, 3): %d, %d.\n", EMA_GETODD(7, 1, 2, 3));
// Prefix all arguments with a value.
EMA_PREFIX(value, args)
printf("EMA_PREFIX(7, 1, 2, 3): %d, %d, %d.\n", EMA_PREFIX(7, 1, 2, 3));
// Postfix all arguments with a value.
EMA_POSTFIX(value, args)
printf("EMA_POSTFIX(7, 1, 2, 3): %d, %d, %d.\n", EMA_POSTFIX(7, 1, 2, 3));
// Merge all arguments.
EMA_MERGE(args)
printf("EMA_MERGE(7, 1, 2, 3): %d.\n", EMA_MERGE(7, 1, 2, 3));
// Merge all arguments pairs.
EMA_MERGEPAIR(args)
printf("EMA_MERGEPAIR(7, 1, 2, 3): %d, %d.\n", EMA_MERGEPAIR(7, 1, 2, 3));
// Join all arguments, with separator.
EMA_JOIN(separator, args)
printf("EMA_JOIN(7, 1, 2, 3): %d.\n", EMA_JOIN(7, 1, 2, 3));
// Join all argument pairs, with separator.
EMA_JOINPAIR(separator, args)
printf("EMA_JOINPAIR(7, 1, 2, 3): %d, %d.\n", EMA_JOINPAIR(7, 1, 2, 3));
// Space arguments, a character apart.
EMA_SPACE(args)
printf("EMA_SPACE(h, b, c, d): %s.\n", EMA_SPACE("h", "b", "c", "d"));
// Space argument pairs, a character apart.
EMA_SPACEPAIR(args)
printf("EMA_SPACEPAIR(h, b, c, d): %s, %s.\n", EMA_SPACEPAIR("h", "b", "c", "d"));