473,320 Members | 2,146 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,320 software developers and data experts.

__FILE__ and __LINE__ within macros

Am I correct that using __FILE__ and __LINE__ within a macro will expand
to the filename and line in which the macro is invoked, rather than where
it is defined?

For example, in a header file:

#ifdef DEBUG
#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#else
#define LOGIT(note)
#endif

And on line 123 of "foo.c", you have:

LOGIT("whatever")

If DEBUG is defined, this will expand to:

DoLogging("foo.c",123,"whatever")

as opposed to:

DoLogging("something.h",42,"whatever")

Is this guaranteed?
What about a macro-within-a-macro, as in:

#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#define LOGMYBUF LOGIT(MyBuf)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Apr 4 '06 #1
7 2645
Kenneth Brody wrote:
Am I correct that using __FILE__ and __LINE__ within a macro will
expand to the filename and line in which the macro is invoked, rather
than where it is defined?

For example, in a header file:

#ifdef DEBUG
#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#else
#define LOGIT(note)
#endif

And on line 123 of "foo.c", you have:

LOGIT("whatever")

If DEBUG is defined, this will expand to:

DoLogging("foo.c",123,"whatever")

as opposed to:

DoLogging("something.h",42,"whatever")

Is this guaranteed?
What about a macro-within-a-macro, as in:

#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#define LOGMYBUF LOGIT(MyBuf)


I can't find a description in the stds that says that your premise is
actually correct - but maybe that's implied in the wording ... e.g., [below]
'source file' /suggests/ that header files aren't in the frame. Of course,
they wouldn't be a whole lot of use if they didn't consistently work the way
you'd like them to, but that's not what you're asking I believe!

6.10.8
_ _FILE_ _ The presumed name of the current source file (a character string
literal).

_ _LINE_ _ The presumed line number (within the current source file) of the
current source line (an integer constant).

--
==============
Not a pedant
==============
Apr 4 '06 #2
Kenneth Brody wrote:
Am I correct that using __FILE__ and __LINE__ within a macro will expand
to the filename and line in which the macro is invoked, rather than where
it is defined?

For example, in a header file:

#ifdef DEBUG
#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#else
#define LOGIT(note)
#endif

And on line 123 of "foo.c", you have:

LOGIT("whatever")

If DEBUG is defined, this will expand to:

DoLogging("foo.c",123,"whatever")

as opposed to:

DoLogging("something.h",42,"whatever")

Is this guaranteed?
Yes. Macro parameters are expanded during invocation, not definition.
What about a macro-within-a-macro, as in:

#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#define LOGMYBUF LOGIT(MyBuf)


Same thing.

Robert Gamble

Apr 4 '06 #3
pemo wrote:
Kenneth Brody wrote:
Am I correct that using __FILE__ and __LINE__ within a macro will
expand to the filename and line in which the macro is invoked, rather
than where it is defined?

For example, in a header file:

#ifdef DEBUG
#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#else
#define LOGIT(note)
#endif

And on line 123 of "foo.c", you have:

LOGIT("whatever")

If DEBUG is defined, this will expand to:

DoLogging("foo.c",123,"whatever")

as opposed to:

DoLogging("something.h",42,"whatever")

Is this guaranteed?
What about a macro-within-a-macro, as in:

#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#define LOGMYBUF LOGIT(MyBuf)


I can't find a description in the stds that says that your premise is
actually correct - but maybe that's implied in the wording ... e.g., [below]
'source file' /suggests/ that header files aren't in the frame. Of course,
they wouldn't be a whole lot of use if they didn't consistently work the way
you'd like them to, but that's not what you're asking I believe!

6.10.8
_ _FILE_ _ The presumed name of the current source file (a character string
literal).

_ _LINE_ _ The presumed line number (within the current source file) of the
current source line (an integer constant).


Are you still teaching C?

Robert Gamble

Apr 5 '06 #4
Robert Gamble wrote:
pemo wrote:
Kenneth Brody wrote:
Am I correct that using __FILE__ and __LINE__ within a macro will
expand to the filename and line in which the macro is invoked,
rather than where it is defined?

For example, in a header file:

#ifdef DEBUG
#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#else
#define LOGIT(note)
#endif

And on line 123 of "foo.c", you have:

LOGIT("whatever")

If DEBUG is defined, this will expand to:

DoLogging("foo.c",123,"whatever")

as opposed to:

DoLogging("something.h",42,"whatever")

Is this guaranteed?
What about a macro-within-a-macro, as in:

#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#define LOGMYBUF LOGIT(MyBuf)


I can't find a description in the stds that says that your premise is
actually correct - but maybe that's implied in the wording ... e.g.,
[below] 'source file' /suggests/ that header files aren't in the
frame. Of course, they wouldn't be a whole lot of use if they
didn't consistently work the way you'd like them to, but that's not
what you're asking I believe!

6.10.8
_ _FILE_ _ The presumed name of the current source file (a character
string literal).

_ _LINE_ _ The presumed line number (within the current source file)
of the current source line (an integer constant).


Are you still teaching C?


Not perfectly, but yes.

--
==============
Not a pedant
==============
Apr 5 '06 #5
Robert Gamble wrote:

Kenneth Brody wrote:
Am I correct that using __FILE__ and __LINE__ within a macro will expand
to the filename and line in which the macro is invoked, rather than where
it is defined? [... snip example ...] Is this guaranteed?


Yes. Macro parameters are expanded during invocation, not definition.
What about a macro-within-a-macro, as in:

#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#define LOGMYBUF LOGIT(MyBuf)


Same thing.


Thanks. For some reason, I was thinking that macros-within-macros were
expanded at definition. (I don't know where I got that idea.)

But, this confirms it:

==========
#include <stdio.h>

#define VALUE 1
#define WRAPPER printf("%d\n",VALUE)
#undef VALUE
#define VALUE 42

main()
{
WRAPPER;
}
==========

The output is "42".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Apr 5 '06 #6
Yes.

Generally for any pre-prosessor macro doubts, you can stop the
compilation
after pre-prosessing to see what's going on. In gcc for example, you
can run:
gcc -E your_file.c
to see the pre-processed output (all your macros expanded at this time)
Other compilers should have a similar option.

Chandra

Apr 5 '06 #7
"Chandra Kalle" <ch***********@gmail.com> writes:
Yes.


Yes to what??

<http://cfaj.freeshell.org/google/>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 5 '06 #8

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

Similar topics

1
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...
9
by: qazmlp | last post by:
How exactly __FILE__ and __LINE__ macros are defined? Or, Is the definition of these macros implementation dependent ? I am wondering how easily they can get the file name and line number...
5
by: baumann.Pan | last post by:
where are these macros defined? can I use it a release ver code?
18
by: Lukas Ruf | last post by:
Dear all, for debugging purposes, I like the pre-compiler macros __FILE__, __FUNCTION__, __LINE__ I have been wondering if there is a short form of __FILE__ that provides only the filename...
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
4
by: Joakim Hove | last post by:
Hello, i have simple function like this: def log_msg(msg , file , line): print "%s:%s %s" % (file,line,msg) the file and line arguments should be the filename and linenumber of the...
5
by: PengYu.UT | last post by:
Hi, I want to print out error message from all the three macros __FILE__, __LINE__, __PRETTY_FUNCTION__. I'm wondering if there is one macro which serves equivalently as the combinations of the...
5
by: Neo | last post by:
Hie, Can I put __FILE__ and __LINE__ macros inside the class function which may not be inline. And void function() { classobject::LogInfo(...); } Internally LogInfo should log file name...
9
by: Neo | last post by:
Hi Friends, I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging component in my class. In debug build I gets all information. I tried with release mode also and it works. But I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.