1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-27 00:29:31 +00:00
cc65/test/ref/stdarg.c

95 lines
2.9 KiB
C
Raw Normal View History

2014-09-24 14:45:10 +00:00
/*
!!DESCRIPTION!! variable argument lists
!!ORIGIN!! LCC 4.1 Testsuite
!!LICENCE!! own, freely distributeable for non-profit. read CPYRIGHT.LCC
*/
#include "common.h"
2014-09-24 14:45:10 +00:00
#include <stdarg.h>
#ifndef NO_FUNCS_TAKE_STRUCTS
struct node
{
2019-02-12 21:50:49 +00:00
int a[4];
2014-09-24 14:45:10 +00:00
} x =
{
#ifdef NO_SLOPPY_STRUCT_INIT
2019-02-12 21:50:49 +00:00
{
2014-09-24 14:45:10 +00:00
#endif
2019-02-12 21:50:49 +00:00
1,2,3,4
2014-09-24 14:45:10 +00:00
#ifdef NO_SLOPPY_STRUCT_INIT
2019-02-12 21:50:49 +00:00
}
2014-09-24 14:45:10 +00:00
#endif
};
#endif
print(char *fmt, ...);
main()
{
2019-02-12 21:50:49 +00:00
print("test 1\n");
print("test %s\n", "2");
print("test %d%c", 3, '\n');
print("%s%s %w%c", "te", "st", 4, '\n');
2014-09-24 14:45:10 +00:00
#ifdef NO_FLOATS
2019-02-12 21:50:49 +00:00
print("%s%s %f%c", "te", "st", (signed long) 5, '\n');
#else
print("%s%s %f%c", "te", "st", 5.0, '\n');
2014-09-24 14:45:10 +00:00
#endif
2019-02-12 21:50:49 +00:00
#ifndef NO_FUNCS_TAKE_STRUCTS
2014-09-24 14:45:10 +00:00
print("%b %b %b %b %b %b\n", x, x, x, x, x, x);
2019-02-12 21:50:49 +00:00
#endif
return 0;
2014-09-24 14:45:10 +00:00
}
print(char *fmt, ...) {
2019-02-12 21:50:49 +00:00
va_list ap;
va_start(ap, fmt);
for (; *fmt; fmt++)
{
if (*fmt == '%')
switch (*++fmt) {
2014-09-24 14:45:10 +00:00
case 'b': {
2019-02-12 21:50:49 +00:00
#ifdef NO_FUNCS_TAKE_STRUCTS
printf("(1 2 3 4)");
#else
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]);
#endif
2014-09-24 14:45:10 +00:00
break;
}
2019-02-12 21:50:49 +00:00
case 'c':
/* printf("%c", va_arg(ap, char)); */
printf("%c", va_arg(ap, int));
break;
case 'd':
printf("%d", va_arg(ap, int));
break;
case 'w':
/* printf("%x", va_arg(ap, short)); */
printf("%x", va_arg(ap, int));
break;
case 's':
printf("%s", va_arg(ap, char *));
break;
case 'f':
#ifdef NO_FLOATS
printf("%ld.000000", va_arg(ap, signed long));
#else
printf("%f", va_arg(ap, double));
#endif
break;
default:
printf("%c", *fmt);
break;
}
else
printf("%c", *fmt);
}
va_end(ap);
2014-09-24 14:45:10 +00:00
}