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

__FILE__ and __LINE__ within macros

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.