Can __LINE__ be stringified?

C

Carlos

I have a macro

#define DIE(msg) do { fprintf(stderr, "%s (l.%d)\n", msg, __LINE__);\
exit(1); }\
while (0)

and it works :). But later I thought, that if I use it like this:

s = malloc(2000); if (!s) DIE("malloc failed!");

it is possible that the fprintf will fail too, trying to allocate memory
to build the output string (or probably not, I don't know the internals;
and also, I think in Linux mallocs never fail, but anyway).

So I thought of using a "simpler" function to display the message,
something like

fputs(msg "\n", stderr)

But how can I add __LINE__?

fputs(msg #__LINE__ "\n", stderr)

doesn't work (the compiler says that "'#' is not followed by a macro
parameter").

Changing DIE do

#define DIE(msg, line) fputs(msg #lines, stderr)

and calling it this way: 'DIE("error", __LINE__);' results in
'fputs("error" "__LINE__", stderr)'.

I want of course 'fputs("error" "145", stderr)' or something like that.

Any ideas?
 
C

Carlos

Carlos wrote:
[...]
fputs(msg #__LINE__ "\n", stderr)

doesn't work (the compiler says that "'#' is not followed by a macro
parameter").

Changing DIE do

#define DIE(msg, line) fputs(msg #lines, stderr)

and calling it this way: 'DIE("error", __LINE__);' results in
'fputs("error" "__LINE__", stderr)'.

I want of course 'fputs("error" "145", stderr)' or something like that.

Any ideas?

Sorry for wasting your time :). Immediatly after posting that I found
the solution:

$ cat pre.c
#define DIE3(msg, line) fputs(msg #line, stderr)
#define DIE2(msg, line) DIE3(msg, line)
#define DIE(msg) DIE2(msg, __LINE__)

DIE("error");
DIE("error");

$ gcc -E pre.c
# 1 "pre.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "pre.c"




fputs("error" "5", stderr);
fputs("error" "6", stderr);
$

:)
 
M

Michael B Allen

Carlos wrote:
Immediatly after posting that I found the solution:

$ cat pre.c
#define DIE3(msg, line) fputs(msg #line, stderr)
#define DIE2(msg, line) DIE3(msg, line)
#define DIE(msg) DIE2(msg, __LINE__)

Or more generally:

#define STR0(s) #s
#define STR1(s) STR0(s)
#define LINE_STRING STR1(__LINE__)

#define DIE(msg) fputs(msg LINE_STRING, stderr)

Mike
 
J

Jasen Betts

I have a macro

#define DIE(msg) do { fprintf(stderr, "%s (l.%d)\n", msg, __LINE__);\
exit(1); }\
while (0)

and it works :). But later I thought, that if I use it like this:

s = malloc(2000); if (!s) DIE("malloc failed!");


Look up the assert macro in assert.h
copy what it does.


Bye.
Jasen
 
M

Michael Wojcik

it is possible that the fprintf will fail too, trying to allocate memory
to build the output string

Or for any other reason. The language doesn't guarantee that output
will succeed. It's possible that there are cases where fprintf would
fail but fputs would succeed; it's even possible, though I think it
unlikely, that your program would encounter one. Personally, I
wouldn't consider it high on my list of potential problems to
address.
and also, I think in Linux mallocs never fail, but anyway).

<OT>
About that you are wrong. man setrlimit.
</OT>

--
Michael Wojcik (e-mail address removed)

The antics which have been drawn together in this book are huddled here
for mutual protection like sheep. If they had half a wit apiece each
would bound off in many directions, to unsimplify the target. -- Walt Kelly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top