__FILE__ and __LINE__ within macros

K

Kenneth Brody

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:[email protected]>
 
P

pemo

Kenneth said:
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).
 
R

Robert Gamble

Kenneth said:
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
 
R

Robert Gamble

pemo said:
Kenneth said:
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
 
P

pemo

Robert said:
pemo said:
Kenneth said:
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.
 
K

Kenneth Brody

Robert said:
Kenneth said:
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:[email protected]>
 
C

Chandra Kalle

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top