__LINE__ and __FILE__ -- a beginner's question
Michael Condict
condict at cs.vu.nl
Mon Oct 16 18:10:45 AEST 1989
In article <6257 at arcturus> evil at arcturus.UUCP (Wade Guthrie) writes:
>For what purpose are the __LINE__ and __FILE__ macros used? Are these
>only useful for the writers of code that, in turn, produces compileable
>code, or is there some use for the applications programmer?
My favorite use in application programs is in the definition of a debugging
macro that, given a Boolean expression, evaluates it and reports an error
message if false. (It usually also does something drastic like intentionally
dumping core.) The __LINE__ and __FILE__ vars provide a handy way to
report a uniquely identify which assertion failed. Example:
#define ASSERT(b) \
if (!b) {
fprintf(stderr,
"Assertion violated in %s, line %d. Goodbye.\n",
__FILE__, __LINE__);
abort();
}
Michael Condict
Vrije University
Amsterdam
More information about the Comp.lang.c
mailing list