Stringified __LINE__

Discussion in 'C Programming' started by Michael B Allen, Feb 28, 2005.

  1. How does the following work and is it portable?

    /*
    this uses a little trick to allow __LINE__ to be stringified
    */
    #define _STRING_LINE_(s) #s
    #define _STRING_LINE2_(s) _STRING_LINE_(s)
    #define __LINESTR__ _STRING_LINE2_(__LINE__)
    #define __location__ __FILE__ ":" __LINESTR__

    Result:

    printf("%s", __location__);

    becomes:

    printf("%s", "t0.c" ":" "27");

    and prints:

    t0.c:27

    Mike

    PS: macros found in talloc.h at junkcode.samba.org
     
    Michael B Allen, Feb 28, 2005
    #1
    1. Advertising

  2. Michael B Allen

    Serve Lau Guest

    "Michael B Allen" <> wrote in message
    news:p...
    > How does the following work and is it portable?
    >
    > /*
    > this uses a little trick to allow __LINE__ to be stringified
    > */
    > #define _STRING_LINE_(s) #s
    > #define _STRING_LINE2_(s) _STRING_LINE_(s)
    > #define __LINESTR__ _STRING_LINE2_(__LINE__)
    > #define __location__ __FILE__ ":" __LINESTR__


    __location__ expands to "bla.c" ":" __LINESTR__
    __LINESTR__ expands to _STRING_LINE2_(42)
    _STRING_LINE2 expands to "42"

    so you have "bla.c" ":" "42" which you can view as "bla.c:42"

    In C you can append string literals like this at compile time.

    One tip. Dont use names which start with underscores

    > Result:
    >
    > printf("%s", __location__);
    >
    > becomes:
    >
    > printf("%s", "t0.c" ":" "27");
    >
    > and prints:
    >
    > t0.c:27
    >
    > Mike
    >
    > PS: macros found in talloc.h at junkcode.samba.org
     
    Serve Lau, Feb 28, 2005
    #2
    1. Advertising

  3. In article <>,
    Michael B Allen <> wrote:
    :How does the following work and is it portable?

    :/*
    : this uses a little trick to allow __LINE__ to be stringified
    :*/
    :#define _STRING_LINE_(s) #s
    :#define _STRING_LINE2_(s) _STRING_LINE_(s)
    :#define __LINESTR__ _STRING_LINE2_(__LINE__)

    Portable from C89 onward.

    The way it work has to do with the details of #, which is magic in
    that it only does one level of argument substituation instead of continuing
    onward until there are not more substituations available.

    The call to _STRING_LINE2_ is not in itself a use of #, so the
    parameter, __LINE__, will have its value substituted before _STRING_LINE
    is called. _STRING_LINE_ then does one level of substitution, stringifying
    the first level value of the dummy parameter, s, and thus stringifying
    the line number.

    If you were to call _STRING_LINE_(__LINE__) directly then because
    _STRING_LINE only does one level of substitution, it would substitute
    the literal string __LINE__ in and would stringify that, producing
    "__LINE__" rather than the quoted equivilent of the line number.

    The ## preprocessor operator has the same kind of magic, so you have to
    go through similar levels of indirection for it.
    --
    Look out, there are llamas!
     
    Walter Roberson, Feb 28, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Imre
    Replies:
    4
    Views:
    662
    Peter Gordon
    Feb 23, 2005
  2. Replies:
    2
    Views:
    527
    Raymond Martineau
    Feb 23, 2005
  3. Spry
    Replies:
    1
    Views:
    981
    Richard Heathfield
    Jul 26, 2003
  4. David Bear

    stringified cPickle

    David Bear, Nov 18, 2005, in forum: Python
    Replies:
    1
    Views:
    259
    Chris Mellon
    Nov 18, 2005
  5. Carlos

    Can __LINE__ be stringified?

    Carlos, Jul 29, 2005, in forum: C Programming
    Replies:
    5
    Views:
    544
    Michael Wojcik
    Aug 1, 2005
Loading...

Share This Page