aboutsummaryrefslogtreecommitdiffstats
path: root/code/tools/lcc/tst/stdarg.c
blob: c6a5338a1db28646eef8c4b7fca6932e58dc1735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdarg.h>

struct node { int a[4]; } x = {1,2,3,4};

print(char *fmt, ...);

main() {
	print("test 1\n");
	print("test %s\n", "2");
	print("test %d%c", 3, '\n');
	print("%s%s %w%c", "te", "st", 4, '\n');
	print("%s%s %f%c", "te", "st", 5.0, '\n');
	print("%b %b %b %b %b %b\n", x, x, x, x, x, x);
	return 0;
}

print(char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	for (; *fmt; fmt++)
		if (*fmt == '%')
			switch (*++fmt) {
			case 'b': {
				struct node x = va_arg(ap, struct node);
				printf("{%d %d %d %d}", x.a[0], x.a[1], x.a[2], x.a[3]);
				break;
				}
			case 'c':
				printf("%c", va_arg(ap, char));
				break;
			case 'd':
				printf("%d", va_arg(ap, int));
				break;
			case 'w':
				printf("%x", va_arg(ap, short));
				break;
			case 's':
				printf("%s", va_arg(ap, char *));
				break;
			case 'f':
				printf("%f", va_arg(ap, double));
				break;
			default:
				printf("%c", *fmt);
				break;
			}
		 else
			printf("%c", *fmt);
	va_end(ap);
}