Preprocessor fight

This is a discussion on Preprocessor fight within the C Programming forums, part of the General Programming Boards category; I have the following sample from my code.... Code: #include <stdio.h> #define D \ #define #define CMD(name,code) D CMD_##name #code" ...

  1. #1
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Preprocessor fight

    I have the following sample from my code....
    Code:
    #include <stdio.h>
    #define D \
    #define
    #define CMD(name,code) D CMD_##name #code" "#name
    
    CMD(    ACK,    069)
    CMD(    CLOSE,  255)
    CMD(    HELP,   131)
    CMD(    IDENT,  099)
    
    int main()
    {
        printf("%s", CMD_HELP);
    }
    But when i try to compile the files using the #defined strings, i get the error:
    test.c:7: error: stray # in program
    Is there a workaround? I know #define #define is generally not very common, but is possible and i would to know how to correctly use such a construction if it is not too much to ask, oh great masters...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You can't #define a #define.

    I forget what the one-pass version is, but I've generally done this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Yes that's great abuse of the preprocessor, but preprocessing my header file is done with no problem, except from the fact that the #define statements generated have an extra space before them which i can't remove so far...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    After preprocessing your file looks like this:
    Code:
     #define CMD_ACK "069"" ""ACK"
     #define CMD_CLOSE "255"" ""CLOSE"
     #define CMD_HELP "131"" ""HELP"
     #define CMD_IDENT "099"" ""IDENT"
    
    int main()
    {
        printf("&#37;s", CMD_HELP);
    }
    Unfortunately, now it seems it is the compilers turn, and #defines are not what it expects to see in the preprocessed file.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    I know, i kept my original idea as a backup, and sed'ed the preprocessed out of gcc to make my final header file. It works fine now.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    >> #define D \ // can't use meta characters in a define.
    #define // *something*
    #define CMD(name,code) D CMD_##name #code" "#name // ??
    cout << CMD( foo, bar ) << endl; // error: expected ';' before CMD_foo

    that's all just syntactically incorrect. what are you trying to do?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    So you are alive after all... Had sent you a pm about your signature.
    What i am trying to do, is use the preprocessor to create a lot of statements of the form:
    Code:
    #define CMD_ACK         "042 ACK"
    #define CMD_CLOSE     "255 CLOSE"
    ...
    and avoid the retyping of the words define and the CMD_xxx suffix.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21