谁能帮忙看下C supports functions that take a variable number of arguments.The va_* family of macros,defined in the standard header file ,providea portable way for a function to examine its arguments.Read the man pages forstdarg(3) to learn how

来源:学生作业学帮网 编辑:学帮网 时间:2024/05/06 17:36:03

谁能帮忙看下
C supports functions that take a variable number of arguments.
The va_* family of macros,defined in the standard header file ,provide
a portable way for a function to examine its arguments.Read the man pages for
stdarg(3) to learn how these work.
A common use of variable arguments is to create printf-like functions.Create a
function ’write to log,’ which is described below:
#include
#include
int loglevel;
/* If _loglevel is >= loglevel,act as if printf(format,...)
* had been called,passing all received arguments.
* Return the number of characters printed.
*/
int write_to_log(int _loglevel,const char *format,...)
{
#include "log-solution.c"
}
int
main()
{
loglevel = 2;
write_to_log(3,"Log works like printf:%s %d %c\n","a string",42,’x’);
write_to_log(1,"This should not appear","a string",42,’x’);
return 0;
那个log-solution.c应该怎么写?

靠,我前段时间还写了一个呢.给你参考下.
#include "MyLog.h"
#ifdef ANDROID
//nothing to do.
#else
#include
#include
void _info(const char *fmt ...)
{
va_list args;
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end(args);
}
void _warning(const char *fmt ...)
{
va_list args;
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end(args);
}
void _debug(const char *fmt ...)
{
va_list args;
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end(args);
}
void _error(const char *fmt ...)
{
va_list args;
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end(args);
}
#endif