我正在创建printlnf
函数,就像printf
打印格式化文本一样,但最后添加了换行符,问题是我传入的数字是垃圾。< / p>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
void printlnf(const char* input_string, ...) {
/* allocate with room for the newline */
char* fmt_str = malloc(strlen(input_string) + 2);
strcpy(fmt_str, input_string);
strcat(fmt_str, "\n");
/* print the string with the variable arguments */
va_list argptr;
va_start(argptr, input_string);
printf(fmt_str, argptr);
/* free everything */
va_end(argptr);
free(fmt_str);
}
int main(void) {
printlnf("This is a test of the printlnf");
printlnf("This is the %dnd line of the print", 2);
return 0;
}
输出通常与此类似:
This is a test of the printlnf
This is the 1415441184nd line of the print
我该如何解决这个问题?