fork download
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3.  
  4. /*** Macros */
  5.  
  6. #define CountOf(array) (sizeof (array) / sizeof *(array))
  7. #define ArgCount(...) (CountOf(((int[]){__VA_ARGS__})))
  8.  
  9. /*** Function declarations */
  10.  
  11. void test(int arg_count, ...);
  12. #define test(...) test(ArgCount(__VA_ARGS__), __VA_ARGS__)
  13.  
  14. /*** Function definitions */
  15.  
  16. int main(void) {
  17. int i, j, k;
  18.  
  19. i = j = k = 42;
  20. test(i, ++j, k++);
  21. return 0;
  22. }
  23.  
  24. #undef test
  25. void test(int arg_count, ...) {
  26. va_list vargs;
  27. int arg;
  28.  
  29. if (!arg_count) {
  30. printf("No arguments passed!\n");
  31. }
  32.  
  33. printf("{ ");
  34. va_start(vargs, arg_count);
  35. while (arg_count--) {
  36. arg = va_arg(vargs, int);
  37. printf("%d, ", arg);
  38. continue;
  39. }
  40. va_end(vargs);
  41. printf("}\n");
  42.  
  43. return;
  44. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
{ 42, 43, 42, }