我想在C程序中包裝printf(當然,實際上是_snprintf,但這個例子比較簡單),並且在獲取變量參數的時候遇到了問題。這裏是我的代碼:在C程序中包裝Printf問題
#include <stdio.h>
#include <stdarg.h>
void works(void)
{
printf("%d\n", 100);
}
void wrap_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
printf(fmt, args);
va_end(args);
}
void broken(void)
{
wrap_printf("%d\n", 100);
}
int main(void)
{
works();
broken();
return 0;
}
這裏是我的輸出:
100
3668388
args變量看起來在我的代碼中調用va_start
後好,但只要我一步到C運行時代碼和他們呼籲va_start
的價值看起來不好。有關我可能做錯什麼的想法?
可能重複的[使用va_list調用printf](http://stackoverflow.com/questions/5977326/call-printf-using-va-list) –