Return to Snippet

Revision: 9685
at November 27, 2008 22:46 by jimfred


Updated Code
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x) 
#define AT __FILE__ ":" TOSTRING(__LINE__)

printf( AT " message"); // Expect output like: "snipplr.c:5 message"

Revision: 9684
at November 17, 2008 19:18 by jimfred


Initial Code
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x) 
#define AT __FILE__ ":" TOSTRING(__LINE__)

printf( AT " fake error");

Initial URL
http://www.decompile.com/cpp/faq/file_and_line_error_string.htm

Initial Description
Article explains why STRINGIFY and TOSTRING are both needed.

This technique is useful for embedded apps to print debug strings to a debug console. ASSERT or TRACE statements can incorporate these macros. The line numbers are converted to strings at compile-time rather than run-time.

Initial Title
print __FILE__ and __LINE__ using the # stringizing operator

Initial Tags
file, line

Initial Language
C++