boot2

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

076-vararg-recv.c (807B)


      1 /* §G.2 — variadic receive: va_start / arg / end.
      2  *
      3  * Mirrors what <stdarg.h> would ship: va_list = char*; va_start /
      4  * va_arg / va_end are macro-aliases for the __builtin_* names. Here
      5  * the test uses the builtins directly, since the cc-parse pipeline
      6  * doesn't fold #include. */
      7 
      8 #ifndef CCSCM
      9 #include <stdarg.h>
     10 #else
     11 typedef char *va_list;
     12 #define va_start(ap, n) __builtin_va_start(ap, n)
     13 #define va_arg(ap, t)   __builtin_va_arg(ap, t)
     14 #define va_end(ap)      __builtin_va_end(ap)
     15 #endif
     16 
     17 int sum(int n, ...) {
     18     va_list ap;
     19     int total;
     20     int i;
     21     va_start(ap, n);
     22     total = 0;
     23     i = 0;
     24     while (i < n) {
     25         total = total + va_arg(ap, int);
     26         i = i + 1;
     27     }
     28     va_end(ap);
     29     return total;
     30 }
     31 
     32 int main(void) {
     33     return sum(3, 5, 7, 9);  /* 5 + 7 + 9 = 21 */
     34 }