By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,368 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,368 IT Pros & Developers. It's quick & easy.

Macro expansion of '#__LINE__'?

P: n/a
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }
struct opinfo {
const char *preamble;
const char *cstr;
};

....

opinfo ops[] = {
OP("wibble1"),
OP("wibble2")
};

---------------

My intention is that this should produce an array of two opinfos which
look like:

opinfo ops[] = {
{ "src.cc, line 10: ", "wibble1" },
{ "src.cc, line 11: ", "wibble2" }
};

However, what it actually produces is:

opinfo ops[] = {
{ "src.cc, line __LINE__: ", "wibble1" },
{ "src.cc, line __LINE__: ", "wibble2" }
};

Any ideas?

Thanks -

Dom
Nov 4 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2005-11-04, Dom Gilligan <dg****@hotmail.com> wrote:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.


#define STRING2(x) #x
#define STRING(x) STRING2(x)
#define STR_LINE STRING(__LINE__)

the stringize operator is weird, but this seems to work.
Nov 4 '05 #2

P: n/a
Dom Gilligan wrote:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }


Arguments of '#' are not considered for further macro replacement. That's why
your __LINE__ is not replaced with the line number. You need an extra macro
level to give __LINE__ an opportunity to get replaced

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) OP3(x, y, z)
#define OP3(x,y,z) { x ", line " #y ": ", z }

--
Best regards,
Andrey Tarasevich
Nov 4 '05 #3

P: n/a
On Fri, 04 Nov 2005 17:09:53 +0000, Dom Gilligan wrote:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }
struct opinfo {
const char *preamble;
const char *cstr;
};

...

opinfo ops[] = {
OP("wibble1"),
OP("wibble2")
};

---------------

My intention is that this should produce an array of two opinfos which
look like:

opinfo ops[] = {
{ "src.cc, line 10: ", "wibble1" },
{ "src.cc, line 11: ", "wibble2" }
};

However, what it actually produces is:

opinfo ops[] = {
{ "src.cc, line __LINE__: ", "wibble1" },
{ "src.cc, line __LINE__: ", "wibble2" }
};

Any ideas?


You need to stringize indirectly:

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " STR(y) ": ", z }
#define STR(a) # a

Victor

P.S. Sorry for the extensive quoting...
Nov 4 '05 #4

P: n/a
Thanks guys - those all worked. That's what I call service!

Dom
Nov 4 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.