I have a variable argument function that prints error messages in my application, the code of which 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 under error conditions as follows:
error("invalid image width %d and image height %d in GIF file %s",wid,hei,name);
The function error()is called from different places with different arguments (variable argument function).
A functional approach works fine.
Now, if I need to convert this function to a macro, how do I do this? I tried to do it like:
printf("Error: ");\
va_start(args, format);\
vfprintf(stderr, format, args);\
va_end(args);\
printf("\n"); abort()}
But this incorrectly prints the arguments.
What is wrong with the macro definition above?
What is a fix?
source
share