Manipulating string constants with preprocessor macros - possible?

Discussion in 'C Programming' started by Veit Wiessner, Dec 3, 2003.

  1. I wrote a program that handles the buildcount of projects (gets called
    every time I compile a project, it writes a header file which is
    #include-ed in the project).
    My question is this, is it possible to modify string constants with
    preprocessor macros? for now I write every string I need as a #define
    in the generated header, when in theory I only need to define the
    buildcount there and put it then in the places where I need it.

    for instance:

    #define BUILD 1


    somewhere in the program I have string constant

    char *version = "XYZ Version 2.3.21 Build X";

    now I want to replace the X with the actual buildcount, but macros
    inside quotationmarks don't work. Is there a way to somehow put the
    quotationmarks around the string constant after the BUILD macro was
    applied? With a macro?

    I hope I made my problem clear, TIA :)

    Veit

    --
    if life throws you lemons make lemonade
     
    Veit Wiessner, Dec 3, 2003
    #1
    1. Advertising

  2. Veit Wiessner

    ram_nrk200 Guest

    Veit Wiessner wrote:

    > I wrote a program that handles the buildcount of projects (gets called
    > every time I compile a project, it writes a header file which is
    > #include-ed in the project).
    > My question is this, is it possible to modify string constants with
    > preprocessor macros? for now I write every string I need as a #define
    > in the generated header, when in theory I only need to define the
    > buildcount there and put it then in the places where I need it.
    >
    > for instance:
    >
    > #define BUILD 1
    >
    >
    > somewhere in the program I have string constant
    >
    > char *version = "XYZ Version 2.3.21 Build X";
    >
    > now I want to replace the X with the actual buildcount, but macros
    > inside quotationmarks don't work. Is there a way to somehow put the
    > quotationmarks around the string constant after the BUILD macro was
    > applied? With a macro?
    >


    #define STRINGIFY(x) #x
    #define BUILD 1

    char *version = "XYZ Version 2.3.21 Build " STRINGIFY(BUILD);

    int main(void) {
    printf("Build is %s\n", version);
    return 0;
    }

    HTH,
    -nrk.

    > I hope I made my problem clear, TIA :)
    >
    > Veit
    >
     
    ram_nrk200, Dec 3, 2003
    #2
    1. Advertising

  3. Veit Wiessner

    nrk Guest

    ram_nrk200 wrote:

    > Veit Wiessner wrote:
    >
    >> I wrote a program that handles the buildcount of projects (gets called
    >> every time I compile a project, it writes a header file which is
    >> #include-ed in the project).
    >> My question is this, is it possible to modify string constants with
    >> preprocessor macros? for now I write every string I need as a #define
    >> in the generated header, when in theory I only need to define the
    >> buildcount there and put it then in the places where I need it.
    >>
    >> for instance:
    >>
    >> #define BUILD 1
    >>
    >>
    >> somewhere in the program I have string constant
    >>
    >> char *version = "XYZ Version 2.3.21 Build X";
    >>
    >> now I want to replace the X with the actual buildcount, but macros
    >> inside quotationmarks don't work. Is there a way to somehow put the
    >> quotationmarks around the string constant after the BUILD macro was
    >> applied? With a macro?
    >>

    >
    > #define STRINGIFY(x) #x
    > #define BUILD 1
    >
    > char *version = "XYZ Version 2.3.21 Build " STRINGIFY(BUILD);
    >
    > int main(void) {
    > printf("Build is %s\n", version);
    > return 0;
    > }
    >
    > HTH,
    > -nrk.
    >


    Apologies for the crap above, should've run the darn thing before posting.
    Here, try this one... it should work:

    #define STRINGIFY(x) #x
    #define EXPAND(x) STRINGIFY(x)
    #define BUILD 1

    char *version = "XYZ Version 2.3.21 Build " EXPAND(BUILD);

    int main(void) {
    printf("Build is %s\n", version);
    return 0;
    }

    -nrk.
     
    nrk, Dec 3, 2003
    #3
  4. nrk wrote:
    >
    > Apologies for the crap above, should've run the darn thing before
    > posting. Here, try this one... it should work:
    >
    > #define STRINGIFY(x) #x
    > #define EXPAND(x) STRINGIFY(x)
    > #define BUILD 1
    >
    > char *version = "XYZ Version 2.3.21 Build " EXPAND(BUILD);
    >
    > int main(void) {
    > printf("Build is %s\n", version);
    > return 0;
    > }
    >
    > -nrk.


    Exactly what I wanted, thank you very much!

    Veit

    --
    if life throws you lemons make lemonade
     
    Veit Wiessner, Dec 3, 2003
    #4
  5. I am interested in knowing how you are incrementing the build count.
    Are you doing it manually or by making use of a 'makefile' that does
    this? and if so how?

    TIA,
    john

    In article <bqjuuo$20fdfl$-berlin.de>, Veit Wiessner wrote:
    > nrk wrote:
    >>
    >> Apologies for the crap above, should've run the darn thing before
    >> posting. Here, try this one... it should work:
    >>
    >> #define STRINGIFY(x) #x
    >> #define EXPAND(x) STRINGIFY(x)
    >> #define BUILD 1
    >>
    >> char *version = "XYZ Version 2.3.21 Build " EXPAND(BUILD);
    >>
    >> int main(void) {
    >> printf("Build is %s\n", version);
    >> return 0;
    >> }
    >>
    >> -nrk.

    >
    > Exactly what I wanted, thank you very much!
    >
    > Veit
    >
     
    John Navil Joseph, Dec 3, 2003
    #5
  6. John Navil Joseph wrote:
    > I am interested in knowing how you are incrementing the build count.
    > Are you doing it manually or by making use of a 'makefile' that does
    > this? and if so how?
    >


    I do it in the makefile, I added a 'buildcount' target (which only
    calls the buildcount program itself) and the obj-targets which include
    the generated header depend on it.


    Veit

    --
    if life throws you lemons make lemonade
     
    Veit Wiessner, Dec 3, 2003
    #6
    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. Sabyasachi Basu
    Replies:
    25
    Views:
    829
    LibraryUser
    Sep 5, 2003
  2. Cronus
    Replies:
    1
    Views:
    718
    Paul Mensonides
    Jul 15, 2004
  3. Jay Wolfe

    preprocessor macros

    Jay Wolfe, Feb 17, 2005, in forum: C++
    Replies:
    4
    Views:
    495
    Walter
    Feb 18, 2005
  4. Sabyasachi Basu

    C preprocessor function macros with empty aguments

    Sabyasachi Basu, Sep 2, 2003, in forum: C Programming
    Replies:
    26
    Views:
    1,433
    LibraryUser
    Sep 5, 2003
  5. .rhavin grobert

    preprocessor: macros in a string...

    .rhavin grobert, Mar 22, 2007, in forum: C Programming
    Replies:
    5
    Views:
    354
    Robert Gamble
    Mar 23, 2007
Loading...

Share This Page