boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

015-variadic.c (733B)


      1 /* tests/cc-e2e/15-variadic.c — variadic function call + va_list iteration.
      2  * Split from 01-kitchen-sink. */
      3 
      4 #ifndef CCSCM
      5 #include <stdarg.h>
      6 #else
      7 typedef char *va_list;
      8 #define va_start(ap, n) __builtin_va_start(ap, n)
      9 #define va_arg(ap, t)   __builtin_va_arg(ap, t)
     10 #define va_end(ap)      __builtin_va_end(ap)
     11 #endif
     12 
     13 int f_variadic(int n, ...) {
     14     va_list ap;
     15     int total;
     16     int i;
     17     va_start(ap, n);
     18     total = 0;
     19     i = 0;
     20     while (i < n) {
     21         total = total + va_arg(ap, int);
     22         i = i + 1;
     23     }
     24     va_end(ap);
     25     return total;
     26 }
     27 
     28 int test_variadic(void) { return f_variadic(4, 1, 2, 3, 4); }   /* 10 */
     29 
     30 int main(int argc, char **argv) {
     31     if (test_variadic() != 10) return 1;
     32     return 0;
     33 }