scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

0009-stdarg.c (1060B)


      1 #include <assert.h>
      2 #include <stdarg.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 /*
      7 TODO: Test va_copy, new c99 extension.
      8 output:
      9 test 1
     10 test 2
     11 end:
     12 */
     13 
     14 void
     15 test(char *s, char *fmt, ...)
     16 {
     17 	va_list va;
     18 	int cnt = 1;
     19 	long long *p;
     20 
     21 	puts(s);
     22 
     23 	va_start(va, fmt);
     24 	while (*fmt) {
     25 		printf("fmt = '%c'\n", *fmt);
     26 		switch (*fmt++) {
     27 		case 'c':
     28 			assert(va_arg(va, int) == cnt++);
     29 			break;
     30 		case 's':
     31 			assert(va_arg(va, int) == cnt++);
     32 			break;
     33 		case 'i':
     34 			assert(va_arg(va, int) == cnt++);
     35 			break;
     36 		case 'l':
     37 			assert(va_arg(va, long) == cnt++);
     38 			break;
     39 		case 'q':
     40 			assert(va_arg(va, long long) == cnt++);
     41 			break;
     42 		case 'p':
     43 			p = va_arg(va, void *);
     44 			assert(*p == cnt++);
     45 			break;
     46 		case 'f':
     47 			assert(va_arg(va, double) == cnt++);
     48 			break;
     49 		default:
     50 			abort();
     51 		}
     52 	}
     53 	va_end(va);
     54 }
     55 
     56 int
     57 main()
     58 {
     59 	char c;
     60 	short s;
     61 	int i;
     62 	long l;
     63 	long long ll;
     64 	float f;
     65 
     66 	c = 1;
     67 	i = 2;
     68 	l = 3;
     69 	ll = 4;
     70 	test("test 1", "cilp", c, i, l, (void *) &ll);
     71 
     72 
     73 	c = 1;
     74 	s = 2;
     75 	ll = 3;
     76 	f = 4.0;
     77 	test("test 2", "csqf", c, s, ll, f);
     78 
     79 	return 0;
     80 }