Preprocessing

Discussion in 'C Programming' started by R.Biloti, Jul 19, 2006.

  1. R.Biloti

    R.Biloti Guest

    Hi folks

    I would like to do something like

    #define data_x1 data_x1_bbj
    #define data_x2 data_x2_bbj
    #define label_xy label_xy_bbj

    Since the suffix bbj can change from time to time, I've tried

    #define XX bbj

    #define data_x1 data_x1_ ## XX
    #define data_x2 data_x2_ ## XX
    #define label_xy label_xy_ ## XX

    but that doesn't produce what I'm willing, that is, XX doesn't get
    replaced
    by bbj in the three define's.

    Any clue how to do that?

    Regards,
    Biloti
    R.Biloti, Jul 19, 2006
    #1
    1. Advertising

  2. R.Biloti

    Mohan Guest

    R.Biloti wrote:
    > Hi folks
    >
    > I would like to do something like
    >
    > #define data_x1 data_x1_bbj
    > #define data_x2 data_x2_bbj
    > #define label_xy label_xy_bbj
    >
    > Since the suffix bbj can change from time to time, I've tried
    >
    > #define XX bbj
    >
    > #define data_x1 data_x1_ ## XX
    > #define data_x2 data_x2_ ## XX
    > #define label_xy label_xy_ ## XX
    >
    > but that doesn't produce what I'm willing, that is, XX doesn't get
    > replaced
    > by bbj in the three define's.
    >
    > Any clue how to do that?


    you may want to do it like this:

    #define DATA_X1(a) data_x1 ## a
    #define DATA_X2(a) data_x2 ## a
    #define DATA_XY(a) data_xy ## a

    and you can use it as - DATA_X1(bbj), DATA_X2(obj), etc

    --Mohan

    >
    > Regards,
    > Biloti
    Mohan, Jul 19, 2006
    #2
    1. Advertising

  3. R.Biloti

    R.Biloti Guest

    Mohan escreveu:

    > R.Biloti wrote:
    > > Hi folks
    > >
    > > I would like to do something like
    > >
    > > #define data_x1 data_x1_bbj
    > > #define data_x2 data_x2_bbj
    > > #define label_xy label_xy_bbj
    > >
    > > Since the suffix bbj can change from time to time, I've tried
    > >
    > > #define XX bbj
    > >
    > > #define data_x1 data_x1_ ## XX
    > > #define data_x2 data_x2_ ## XX
    > > #define label_xy label_xy_ ## XX
    > >
    > > but that doesn't produce what I'm willing, that is, XX doesn't get
    > > replaced
    > > by bbj in the three define's.
    > >
    > > Any clue how to do that?

    >
    > you may want to do it like this:
    >
    > #define DATA_X1(a) data_x1 ## a
    > #define DATA_X2(a) data_x2 ## a
    > #define DATA_XY(a) data_xy ## a
    >
    > and you can use it as - DATA_X1(bbj), DATA_X2(obj), etc
    >
    > --Mohan


    But that implies that I should type bbj wherever I use the macros
    DATA_X1, DATA_X2, etc, and therefore, when I change bbj to kkt, I would
    have to search-and-replace in the entire code, which is exactly what I
    as trying to avoid.

    Biloti
    R.Biloti, Jul 19, 2006
    #3
  4. "R.Biloti" <> wrote in message
    news:...
    > Mohan escreveu:
    >
    >> R.Biloti wrote:
    >> > Hi folks
    >> >
    >> > I would like to do something like
    >> >
    >> > #define data_x1 data_x1_bbj
    >> > #define data_x2 data_x2_bbj
    >> > #define label_xy label_xy_bbj
    >> >
    >> > Since the suffix bbj can change from time to time, I've tried
    >> >
    >> > #define XX bbj
    >> >
    >> > #define data_x1 data_x1_ ## XX
    >> > #define data_x2 data_x2_ ## XX
    >> > #define label_xy label_xy_ ## XX
    >> >
    >> > but that doesn't produce what I'm willing, that is, XX doesn't get
    >> > replaced
    >> > by bbj in the three define's.
    >> >
    >> > Any clue how to do that?

    >>
    >> you may want to do it like this:
    >>
    >> #define DATA_X1(a) data_x1 ## a
    >> #define DATA_X2(a) data_x2 ## a
    >> #define DATA_XY(a) data_xy ## a
    >>
    >> and you can use it as - DATA_X1(bbj), DATA_X2(obj), etc
    >>
    >> --Mohan

    >
    > But that implies that I should type bbj wherever I use the macros
    > DATA_X1, DATA_X2, etc, and therefore, when I change bbj to kkt, I would
    > have to search-and-replace in the entire code, which is exactly what I
    > as trying to avoid.
    >
    > Biloti
    >


    or you could have another macro
    #define SUFF bbj

    and use DATA_X1(SUFF), DATA_X2(SUFF) then all you need to do is change the
    define for SUFF to avoid searching through the whole code
    Allan
    Allan M. Bruce, Jul 19, 2006
    #4
  5. R.Biloti

    Mohan Guest

    R.Biloti wrote:
    > Mohan escreveu:
    >
    > > R.Biloti wrote:
    > > > Hi folks
    > > >
    > > > I would like to do something like
    > > >
    > > > #define data_x1 data_x1_bbj
    > > > #define data_x2 data_x2_bbj
    > > > #define label_xy label_xy_bbj
    > > >
    > > > Since the suffix bbj can change from time to time, I've tried
    > > >
    > > > #define XX bbj
    > > >
    > > > #define data_x1 data_x1_ ## XX
    > > > #define data_x2 data_x2_ ## XX
    > > > #define label_xy label_xy_ ## XX
    > > >
    > > > but that doesn't produce what I'm willing, that is, XX doesn't get
    > > > replaced
    > > > by bbj in the three define's.
    > > >
    > > > Any clue how to do that?

    > >
    > > you may want to do it like this:
    > >
    > > #define DATA_X1(a) data_x1 ## a
    > > #define DATA_X2(a) data_x2 ## a
    > > #define DATA_XY(a) data_xy ## a
    > >
    > > and you can use it as - DATA_X1(bbj), DATA_X2(obj), etc
    > >
    > > --Mohan

    >
    > But that implies that I should type bbj wherever I use the macros
    > DATA_X1, DATA_X2, etc, and therefore, when I change bbj to kkt, I would
    > have to search-and-replace in the entire code, which is exactly what I
    > as trying to avoid.


    No, you need to have one more set of macros, like (can be placed in a
    header file):
    #define DATA_X1(a) data_x1 ## a
    #define DDATA_X1 DATA_X1(obj)

    #define DATA_X2(a) data_x2 ## a
    #define DDATA_X2 DATA_X2(obj)

    etc

    the macros DDATA_* are used in the rest of the code, hence the changes
    are centralized.

    --Mohan
    >
    > Biloti
    Mohan, Jul 19, 2006
    #5
  6. R.Biloti

    SM Ryan Guest

    "R.Biloti" <> wrote:
    # Hi folks
    #
    # I would like to do something like
    #
    # #define data_x1 data_x1_bbj
    # #define data_x2 data_x2_bbj
    # #define label_xy label_xy_bbj
    #
    # Since the suffix bbj can change from time to time, I've tried
    #
    # #define XX bbj
    #
    # #define data_x1 data_x1_ ## XX
    # #define data_x2 data_x2_ ## XX
    # #define label_xy label_xy_ ## XX
    #
    # but that doesn't produce what I'm willing, that is, XX doesn't get
    # replaced
    # by bbj in the three define's.
    #
    # Any clue how to do that?

    You have to #define two levels down.

    t.c:
    #define data_x1 CONCAT(data_x1_,XX)
    #define data_x2 CONCAT(data_x2_,XX)
    #define label_xy CONCAT(label_xy_,XX)

    #define XX bbj

    #define CONCAT(a,b) CONCAT2(a,b)
    #define CONCAT2(a,b) a##b

    x1 = data_x1
    x2 = data_x2
    xy = label_xy

    cc -E t.c
    # 1 "t.c"
    # 1 "<built-in>"
    # 1 "<command line>"
    # 1 "t.c"
    # 10 "t.c"
    x1 = data_x1_bbj
    x2 = data_x2_bbj
    xy = label_xy_bbj



    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    GERBILS
    GERBILS
    GERBILS
    SM Ryan, Jul 20, 2006
    #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. Dave
    Replies:
    2
    Views:
    346
    Jack Klein
    Nov 3, 2003
  2. Ron
    Replies:
    3
    Views:
    313
    Jack Klein
    Nov 12, 2003
  3. Steven T. Hatton
    Replies:
    7
    Views:
    2,344
    Howard
    Jun 3, 2004
  4. Victor Bazarov
    Replies:
    0
    Views:
    425
    Victor Bazarov
    May 10, 2006
  5. Mark A. Gibbs
    Replies:
    0
    Views:
    3,519
    Mark A. Gibbs
    May 10, 2006
Loading...

Share This Page