I have created a macro for error tracing.
Here is a simplified version:
#include <stdio.h>
#define ERR(...) \
printf("error @ %d\n", __LINE__)
int main()
{
return ERR(1, /* line 7 */
2, /* line 8 */
3); /* line 10 */
}
When executed, it prints:
error @ 10
However, to match the printed line number with grep output (grep -n ERR test.c), I need line number of the ERR string (line 7).
Is such thing even possible? Any ideas?
Additional notes:
the macro should look like a function call (so I can do return ERR(...);).
The compiler is GCC version 4.4.5. C99 + GNU extensions can be used.
Bases on @cwyang proposition, use macros with mismatched parenthesis in the definition and the coma operator.
will expand to
with gcc.