# and ## operators

Discussion in 'C Programming' started by Mantorok Redgormor, Sep 21, 2003.

  1. When is it appropriate to use # and ##? I have been told that ## is
    not useful at all.






    nethlek
    Mantorok Redgormor, Sep 21, 2003
    #1
    1. Advertising

  2. In 'comp.lang.c', (Mantorok Redgormor) wrote:

    > When is it appropriate to use # and ##? I have been told that ## is
    > not useful at all.


    I use it every day. For example, it very useful for local debug macros.

    Say, I have a structure that represent some configuration data:

    typedef struct
    {
    unsigned int foo;
    some_enum_type bar;
    unsigned long baz;
    }
    config_s;

    At a certain stage of my code, I want a trace of the values on the console.

    Ok, I can write

    printf ("foo = %u", p->foo);
    printf ("bar = %u", (unsigned int) p->bar);
    printf ("baz = %ul", p->baz);

    which is rapidly boring is I have 20 elements or more...

    What can be done to help and avoid errors is to ask the proprocessor to
    write the code for you:

    #define PRT(a) \
    printf ("%4s = %ul", #baz, (unsigned long) p-> ## a)

    PRT (foo);
    PRT (bar);
    PRT (baz);

    #undef PRT

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Sep 21, 2003
    #2
    1. Advertising

  3. Mantorok Redgormor

    Kevin Easton Guest

    Emmanuel Delahaye <> wrote:
    > In 'comp.lang.c', (Mantorok Redgormor) wrote:
    >
    >> When is it appropriate to use # and ##? I have been told that ## is
    >> not useful at all.

    >
    > I use it every day. For example, it very useful for local debug macros.
    >
    > Say, I have a structure that represent some configuration data:
    >
    > typedef struct
    > {
    > unsigned int foo;
    > some_enum_type bar;
    > unsigned long baz;
    > }
    > config_s;
    >
    > At a certain stage of my code, I want a trace of the values on the console.
    >
    > Ok, I can write
    >
    > printf ("foo = %u", p->foo);
    > printf ("bar = %u", (unsigned int) p->bar);
    > printf ("baz = %ul", p->baz);
    >
    > which is rapidly boring is I have 20 elements or more...
    >
    > What can be done to help and avoid errors is to ask the proprocessor to
    > write the code for you:
    >
    > #define PRT(a) \
    > printf ("%4s = %ul", #baz, (unsigned long) p-> ## a)


    I think you mean #a there. In the expression p->bar, there are three
    tokens: "p", "->" and "bar". So you don't need the ## token pasting
    operator here - this definition works:

    #define PRT(a) \
    printf ("%4s = %ul", #a, (unsigned long) p-> a)

    To the OP: the ## operator is needed whenever you need to create a
    single token from multiple tokens - this only ever occurs in macros.
    Say you had some code like this:

    func_a(s->data_a);
    func_b(s->data_b);
    func_c(s->data_c);
    func_d(s->data_d);
    func_e(s->data_e);

    and you wanted to use a macro to make it less error-prone. You could do
    that like this:

    #define DO_THING(x) func_ ## x (s -> data ## x)

    DO_THING(a);
    DO_THING(b);
    DO_THING(c);
    DO_THING(d);
    DO_THING(e);

    - Kevin.
    Kevin Easton, Sep 21, 2003
    #3
  4. In 'comp.lang.c', Kevin Easton <> wrote:

    >> #define PRT(a) \
    >> printf ("%4s = %ul", #baz, (unsigned long) p-> ## a)

    >
    > I think you mean #a there.


    Oops, absolultly. Yet another victim of copy and paste...

    > In the expression p->bar, there are three
    > tokens: "p", "->" and "bar". So you don't need the ## token pasting
    > operator here - this definition works:


    True enough, my example was poor.

    > #define PRT(a) \
    > printf ("%4s = %ul", #a, (unsigned long) p-> a)


    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Sep 21, 2003
    #4
  5. Mantorok Redgormor

    mbs Guest

    usage of the #

    > When is it appropriate to use # and ##?

    the # operator helps you harness the power of the c-preprocessor.

    suppose you define a macro like this:
    #define SHOW_INT( var ) printf( #var"=%d\n", var );

    and call it later:

    int i=800;
    ...
    SHOW_INT( i );
    ...

    it would expend to
    printf( "i""=%d\n", i );
    and would print
    i=800
    to stdout.

    Here is my version of SHOW, which I use daily:
    #define SHOW( val, fmt ) { fprintf( stderr, "%d "#val"=#fmt"\n",
    __LINE__, (val) ); }

    this macro prints the what it is about to do to stderr and the does
    it:
    #define DODBG( cmd ) { fprintf( stderr, "%d doing "#cmd"\n",
    __LINE__ ); {cmd;} }
    ..

    > I have been told that ## is not useful at all


    It is. There's an example in K&R, but the book is not here now.

    Michael.
    mbs, Sep 21, 2003
    #5
  6. Mantorok Redgormor

    Jack Klein Guest

    On 20 Sep 2003 22:53:14 -0700, (Mantorok Redgormor)
    wrote in comp.lang.c:

    > When is it appropriate to use # and ##? I have been told that ## is
    > not useful at all.


    On the other hand, my dental hygienist told me just last week that ##
    is very useful.

    Perhaps the person who told you this is not useful at all.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Sep 21, 2003
    #6
  7. Mantorok Redgormor wrote:

    > When is it appropriate to use # and ##? I have been told that ## is
    > not useful at all.



    Mantorok,

    Thank you for your post. I don't know if ## is useful at all.
    The following program uses ##, but not in a very useful way.


    #include <stdio.h>
    #include <string.h>

    #define paste(front, back) front ## back /* p. 92, K&R2 */

    int main()
    {
    char *paste(Rama, nujan); /* char *Ramanujan; */

    strcpy(Ramanujan, "Ramanujan is great");

    printf("%s\n", paste(Rama, nujan));

    return 0;
    }

    Output of program: Ramanujan is great
    Aborted

    Why does this program's output contain two streams (stdout and stderr)?

    --Steve
    Steve Zimmerman, Sep 22, 2003
    #7
  8. Steve Zimmerman <> wrote:

    >Mantorok Redgormor wrote:
    >
    >> When is it appropriate to use # and ##? I have been told that ## is
    >> not useful at all.

    >
    >
    >Mantorok,
    >
    >Thank you for your post. I don't know if ## is useful at all.
    >The following program uses ##, but not in a very useful way.
    >
    >
    >#include <stdio.h>
    >#include <string.h>
    >
    >#define paste(front, back) front ## back /* p. 92, K&R2 */
    >
    >int main()
    >{
    > char *paste(Rama, nujan); /* char *Ramanujan; */
    >
    > strcpy(Ramanujan, "Ramanujan is great");


    You failed to allocate some memory for 'Ramanujan' to point to.

    >
    > printf("%s\n", paste(Rama, nujan));
    >
    > return 0;
    >}
    >
    >Output of program: Ramanujan is great
    > Aborted
    >
    >Why does this program's output contain two streams (stdout and stderr)?


    The abort message, induced by the error above, was printed to stderr.

    Regards

    Irrwahn
    --
    My other computer is a abacus.
    Irrwahn Grausewitz, Sep 22, 2003
    #8
    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. JamesW

    Strings and Operators

    JamesW, Feb 20, 2004, in forum: Perl
    Replies:
    2
    Views:
    462
  2. Dean Mitchell
    Replies:
    4
    Views:
    375
    Attila Feher
    Aug 27, 2003
  3. Philip Müller
    Replies:
    4
    Views:
    102
    Robert Klemme
    Apr 18, 2009
  4. Iain Barnett
    Replies:
    0
    Views:
    84
    Iain Barnett
    Feb 2, 2011
  5. Replies:
    0
    Views:
    81
Loading...

Share This Page