473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Macro expansion of '#__LINE__'?

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
4 8335
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
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
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
Thanks guys - those all worked. That's what I call service!

Dom
Nov 4 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Caleb Hattingh | last post by:
Hi Here is a script I want to be able to write (explanation appears after): *** start of script *** import MyCustomMacroLib # This does the magic I would like help for. # This is not...
3
by: Ark | last post by:
Hello, NG, Please, help on this snippet: #define CAT(a,b) a##b #define COMMENT CAT(/,/) COMMENT This is a comment Should it compile? It passes MS C/C++ 13.0 (Visual Studio 2002) and fails...
7
by: reppisch | last post by:
Hi Ng, i am looking for a method of expanding a macro while the rest of the code remains untouched. I have some code which does macro voodo / ifdef's which i would like to strip and simplify. ...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
2
by: srinu.fsl | last post by:
there's a MACRO call : MACRO1(cnf) and its expansion is : #define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err = SUCCESS)); #define CLEANUP(a)
3
by: casul | last post by:
Hi All, I was told there were a few macro gurus on this group :) I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE )
0
by: borophyll | last post by:
Hi all Can anyone explain to me the algorithm for macro expansion in the C++ preprocessor. I am confused why the following code works like it does: #define A(x) #x #define B(x) A(x)...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
5
by: Srinivas Mudireddy | last post by:
Hi, We have bunch of message levels and depending on whether that level is turned on, messages of that level are printed. For example, we have levels like MSG_LOW, MSG_MED etc. We want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.