C preprocessor problem

Discussion in 'C Programming' started by gpearman@gmail.com, Nov 12, 2007.

  1. Guest

    Hi all,

    I'm trying to generate a static string from two defines that are
    passed in on the command line during make (via -D). I'll admit that my
    understanding of the C preprocessor is not great ,and I can't work out
    how to join the two together at compile time. At the moment I've tried

    static const U8BIT *app_sw_ver_no_str =
    (U8BIT*)#APP_VERSION_STRING##APP_DATE_STRING

    But this fails with gcc. Could anybody tell me how I can join the two
    defines into a string?

    Thanks,
    Gaz.
    , Nov 12, 2007
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi all,
    >
    > I'm trying to generate a static string from two defines that are
    > passed in on the command line during make (via -D). I'll admit that my
    > understanding of the C preprocessor is not great ,and I can't work out
    > how to join the two together at compile time. At the moment I've tried
    >
    > static const U8BIT *app_sw_ver_no_str =
    > (U8BIT*)#APP_VERSION_STRING##APP_DATE_STRING
    >
    > But this fails with gcc. Could anybody tell me how I can join the two
    > defines into a string?


    C automatically merges adjacent string literals separated only by
    whitespace. Thus, "Hello, " "world!" is essentially the same as as
    "Hello, world!". Therefore, it's actually much simpler than think.
    All you have to do is:

    static const U8BIT *app_sw_ver_no_str =
    (U8BIT*) APP_VERSION_STRING APP_DATE_STRING;

    Note: the # and ## operators are useful only within the replacement
    list for the #define of a function-like macro.
    , Nov 12, 2007
    #2
    1. Advertising

  3. said:

    > Hi all,
    >
    > I'm trying to generate a static string from two defines that are
    > passed in on the command line during make (via -D). I'll admit that my
    > understanding of the C preprocessor is not great ,and I can't work out
    > how to join the two together at compile time. At the moment I've tried
    >
    > static const U8BIT *app_sw_ver_no_str =
    > (U8BIT*)#APP_VERSION_STRING##APP_DATE_STRING
    >
    > But this fails with gcc. Could anybody tell me how I can join the two
    > defines into a string?


    http://c-faq.com/ansi/stringize.html

    Note especially the sentence beginning "Note that both # and ## operate
    only..."

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 12, 2007
    #3
  4. Old Wolf Guest

    On Nov 13, 11:49 am, wrote:
    > wrote:
    > > Hi all,

    >
    > > I'm trying to generate a static string from two defines that are
    > > passed in on the command line during make (via -D).

    >
    > All you have to do is:
    >
    > static const U8BIT *app_sw_ver_no_str =
    > (U8BIT*) APP_VERSION_STRING APP_DATE_STRING;


    That assumes that the defines include quotes already,
    e.g. -DAPP_VERSION_STRING="foo" . If not, then the OP
    will have to use a stringize macro as well, e.g.

    static const U8BIT *app_sw_ver_no_str = (U8BIT*)
    STRINGIZE(APP_VERSION_STRING) STRINGIZE(APP_DATE_STRING);
    Old Wolf, Nov 12, 2007
    #4
  5. Guest

    Old Wolf wrote:
    > On Nov 13, 11:49 am, wrote:
    > > wrote:
    > > > Hi all,

    > >
    > > > I'm trying to generate a static string from two defines that are
    > > > passed in on the command line during make (via -D).

    > >
    > > All you have to do is:
    > >
    > > static const U8BIT *app_sw_ver_no_str =
    > > (U8BIT*) APP_VERSION_STRING APP_DATE_STRING;

    >
    > That assumes that the defines include quotes already,


    It figured that two macros both named with a _STRING suffix, intended
    for use in C code, would have values that already had double quotes
    around them. Still, I suppose it is possible.
    , Nov 12, 2007
    #5
    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. Cronus
    Replies:
    1
    Views:
    648
    Paul Mensonides
    Jul 15, 2004
  2. Johnny Willemsen

    Preprocessor problem with Visual Age 6

    Johnny Willemsen, May 2, 2005, in forum: C++
    Replies:
    0
    Views:
    337
    Johnny Willemsen
    May 2, 2005
  3. onkar

    preprocessor problem

    onkar, Jun 6, 2006, in forum: C Programming
    Replies:
    3
    Views:
    354
    Suman
    Jun 6, 2006
  4. Henry Townsend

    token pasting problem in K&R preprocessor

    Henry Townsend, Jun 19, 2006, in forum: C Programming
    Replies:
    13
    Views:
    640
  5. Subra
    Replies:
    7
    Views:
    423
    Subra
    Jul 4, 2007
Loading...

Share This Page