__FILE__ and __LINE__ within macros

Discussion in 'C Programming' started by Kenneth Brody, Apr 4, 2006.

  1. 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:>
     
    Kenneth Brody, Apr 4, 2006
    #1
    1. Advertisements

  2. Kenneth Brody

    pemo Guest

    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).
     
    pemo, Apr 4, 2006
    #2
    1. Advertisements

  3. Yes. Macro parameters are expanded during invocation, not definition.
    Same thing.

    Robert Gamble
     
    Robert Gamble, Apr 4, 2006
    #3
  4. Are you still teaching C?

    Robert Gamble
     
    Robert Gamble, Apr 5, 2006
    #4
  5. Kenneth Brody

    pemo Guest

    Not perfectly, but yes.
     
    pemo, Apr 5, 2006
    #5
  6. 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:>
     
    Kenneth Brody, Apr 5, 2006
    #6
  7. 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
     
    Chandra Kalle, Apr 5, 2006
    #7
  8. Keith Thompson, Apr 5, 2006
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.