I have a variable argument function which prints error messages in my application, whose code is given below:
void error(char *format,...)
{ va_list args;
printf("Error: ");
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
printf("\n");
abort();
}
This function is used in error conditions as follows:
error("invalid image width %d and image height %d in GIF file %s",wid,hei,name);
The error()
function is called from different places with different arguments (variable argument function).
The function approach works fine.
Now, if I have to convert this function into a macro, how do I do it? I tried doing it as:
#define error(format) {va_list args;\
printf("Error: ");\
va_start(args, format);\
vfprintf(stderr, format, args);\
va_end(args);\
printf("\n"); abort()}
But this does not print the arguments correctly.
What is wrong in the macro definition above?
What is the fix?