我的打印功能使用va_arg函数打印随机字符。我无法弄清楚为什么它不起作用。感谢您的帮助!
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
int myprint(const char *str, ...) {
va_list args;
int counter = 0;
va_start (args, str);
while (*str) {
if (*str == '&') {
fputc(va_arg(args, int), stdout);
counter++;
} else {
fputc(*str, stdout);
}
str++;
}
va_end(args);
fputc('\0', stdout);
return counter;
}
int main(void)
{
myprint("Hello!\n");
myprint("Number: &\n", 5);
myprint("Number one: &, number two: &\n", 120, 1345);
int ret = myprint("Three numbers: & & &\n", 12, 444, 5555);
myprint("I just printed & integers\n", ret);
return 0;
}