473,804 Members | 2,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stringification of __LINE__: why two passes?

Can someone kindly explain why stringification of the compiler
preprocessor macro __LINE__ requires two steps, instead of one? I
wanted to pass the error location of a system call to perror() and I
found that I had to use a kludgy way to stick in the line number.

Thanks,
Song

///// Code Snippet /////
#include <stdio.h>
#include <errno.h>
#include <string>

using namespace std;

#define mkstr1(X) #X
#define mkstr2(X) mkstr1(X)

#define INVALID_FD 0x10000000

main()
{
int rc;
if((rc=read(INV ALID_FD, NULL, 0)) < 0)
{
string errLoc = string(__FILE__ ) + string(", ") +
string(mkstr2(_ _LINE__));
perror(errLoc.c _str());
}
}

Oct 18 '06 #1
5 2986
Generic Usenet Account wrote:
Can someone kindly explain why stringification of the compiler
preprocessor macro __LINE__ requires two steps, instead of one? I
wanted to pass the error location of a system call to perror() and I
found that I had to use a kludgy way to stick in the line number.
Macros are not recursively expanded if the expansion contains # or ##.
That's IIRC, of course.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 18 '06 #2
Generic Usenet Account wrote:
...
Can someone kindly explain why stringification of the compiler
preprocessor macro __LINE__ requires two steps, instead of one? I
wanted to pass the error location of a system call to perror() and I
found that I had to use a kludgy way to stick in the line number.
...
When you use #/## operators in macro definition, macro parameters adjacent to
#/## are substituted with actual argument tokens, but no further recursive macro
replacement takes place. This means that if you use another macro as the actual
argument, this macro will not be recursively replaced.

For example, in your case, if you use 'mkstr1(__LINE_ _)' what you'll get is
string literal "__LINE__", which normally is not the desired result. But if you
use 'mkstr2(__LINE_ _)' the recursive replacement of '__LINE__' with the actual
line number will take place early (at the "first pass") and by the time it gets
to # it will already be converted to a number.

--
Best regards,
Andrey Tarasevich
Oct 18 '06 #3
Generic Usenet Account wrote:
>
Can someone kindly explain why stringification of the compiler
preprocessor macro __LINE__ requires two steps, instead of one? I
wanted to pass the error location of a system call to perror() and I
found that I had to use a kludgy way to stick in the line number.

///// Code Snippet /////
#include <stdio.h>
#include <errno.h>
#include <string>

using namespace std;
This is a syntax error in C. You probably want comp.lang.c++.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

Oct 19 '06 #4
Generic Usenet Account wrote:
Can someone kindly explain why stringification of the compiler
preprocessor macro __LINE__ requires two steps, instead of one? I
wanted to pass the error location of a system call to perror() and I
found that I had to use a kludgy way to stick in the line number.

///// Code Snippet /////
#include <stdio.h>
#include <errno.h>
#include <string>

using namespace std;

#define mkstr1(X) #X
#define mkstr2(X) mkstr1(X)
....
string errLoc = string(__FILE__ ) + string(", ") +
string(mkstr2(_ _LINE__));
This is so that it is possible to convert the argument without macro
substitution. For example:

#include <assert.h>
#define IN_RANGE(x) ((x) 5 && (x) < 10))
....
assert (IN_RANGE(x));

The assert macro can use the # operator to convert the argument
IN_RANGE(x) to "IN_RANGE(x )" rather than the macro expanded version.

If you want the macro substitution first, you must invoke another macro
which has the # operator, as you did. The substitution happens as part
of the first macro expansion since it does not have a preceding # or
adjacent ## preprocessor token.

--
Thad
Oct 25 '06 #5
CBFalconer wrote:
Generic Usenet Account wrote:

using namespace std;

This is a syntax error in C. You probably want comp.lang.c++.
Ironically, the implicit int for main also makes this invalid in C++.

Regards,
Bart.

Oct 25 '06 #6

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

Similar topics

4
2378
by: Imre | last post by:
Is there a Visual C++ newsgroup? I guess this question should go there, but I couldn't find it. Please take a look at the following little program: template <int Line> struct Test { enum { value = Line }; };
4
8370
by: Dom Gilligan | last post by:
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: ---------------
1
6950
by: Spry | last post by:
Hi, I wanted to write macros for finding the number of memory allocations and deallocations also wanted to find the locations. The code I have is a pretty big one. I have a wrapper on top of malloc(int x) as Xmalloc(int x) Now I want to write a macro for Xmalloc which can log the location of the file and the line and the number of bytes allocated.
18
11397
by: Paul Shipley | last post by:
Hi, Does anyone know a way of converting the __LINE__ macro to a string at compile time? The reason I ask it because I want to put some debug information in to tell me if memory is not being allocated. For example, this function will return the status of my system: static char* get_status (void) { char* str_ptr;
5
12097
by: jake1138 | last post by:
I couldn't find an example of this anywhere so I post it in the hope that someone finds it useful. I believe this is compiler specific (I'm using gcc), as C99 defines __VA_ARGS__. Comments are welcome. This will print the file name and line number followed by a format string and a variable number of arguments. The key here is that you MUST have a space between __LINE__ and the last comma, otherwise __LINE__ gets eaten by the ## if...
5
1791
by: Carlos | last post by:
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!");
9
2344
by: Francois Grieu | last post by:
Hello, I wrote this: #define M(x) enum { m##__LINE__ = x }; #line 1000 M(126) M(341) M(565) ...
5
579
by: Generic Usenet Account | last post by:
Can someone kindly explain why stringification of the compiler preprocessor macro __LINE__ requires two steps, instead of one? I wanted to pass the error location of a system call to perror() and I found that I had to use a kludgy way to stick in the line number. Thanks, Song ///// Code Snippet ///// #include <stdio.h>
3
7818
by: pistmaster | last post by:
Hi, I am trying the use the current line number in my logging and I want to stringify it so that I know the size of the buffer required to output the log string. I would have though I could use #__LINE__ in a macro like: #define LOG_ERROR(err) LogError(err, __FILE__, #__LINE__) but all i get in the output is #<linenumber>. Why should this be?
3
14581
by: travis.downs | last post by:
Hi, I'm trying to use a macro to create a unique temporary variable name, such as #define TEMP_OBJ(string) MyType obj_ <some magic here(string); So something like TEMP_OBJ("foo")
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.