generating macros with another macro

Discussion in 'C Programming' started by rhXX, Jun 18, 2007.

  1. rhXX

    rhXX Guest

    hi all,

    i want to define a group of consts for bits

    #define BIT0 (1 << 0)
    #define BIT1 (1 << 1)
    ....
    #define BITi (1 << i)

    is it a way to do it more elegant? or at least to put "i" as parameter
    of other macro?

    how i can do some like this???

    #define BITX(i) #define BIT##i (1 << i)


    tks in advance
    rhXX, Jun 18, 2007
    #1
    1. Advertising

  2. rhXX

    Eric Sosman Guest

    rhXX wrote On 06/18/07 12:13,:
    > hi all,
    >
    > i want to define a group of consts for bits
    >
    > #define BIT0 (1 << 0)
    > #define BIT1 (1 << 1)
    > ...
    > #define BITi (1 << i)
    >
    > is it a way to do it more elegant? or at least to put "i" as parameter
    > of other macro?
    >
    > how i can do some like this???
    >
    > #define BITX(i) #define BIT##i (1 << i)


    You cannot: A macro cannot generate a preprocessor
    directive, even if the expansion resembles one.

    Even if you could, what help would it be? Instead
    of the group of #defines above, you'd have

    #define BITX(i) ...something magical...
    BITX(0)
    BITX(1)
    ...
    BITX(i)

    That is, you'd need one *more* line than you already have.

    Since the names of your macros are so descriptive ;-)
    why not just use

    #define BIT(i) (1 << (i)) /* maybe 1u? 1uL? 1uLL? */

    .... and write BIT(0), BIT(2) instead of BIT0, BIT2?

    --
    Eric Sosman, Jun 18, 2007
    #2
    1. Advertising

  3. rhXX

    CryptiqueGuy Guest

    On Jun 18, 9:13 pm, rhXX <> wrote:
    > hi all,
    >
    > i want to define a group of consts for bits
    >
    > #define BIT0 (1 << 0)
    > #define BIT1 (1 << 1)
    > ...
    > #define BITi (1 << i)
    >
    > is it a way to do it more elegant? or at least to put "i" as parameter
    > of other macro?


    You can write the code, for generating such sequence #define
    BIT0..etc. and print it in the required .c file.
    This is the only possible solution I envisage.

    >
    > how i can do some like this???
    >
    > #define BITX(i) #define BIT##i (1 << i)
    >
    > tks in advance
    CryptiqueGuy, Jun 18, 2007
    #3
  4. rhXX

    rhXX Guest

    tks eric and CryptiqueGuy

    On Jun 18, 6:28 pm, Eric Sosman <> wrote:

    > You cannot: A macro cannot generate a preprocessor
    > directive, even if the expansion resembles one.


    > #define BITX(i) ...something magical...
    > BITX(0)
    > Since the names of your macros are so descriptive ;-)
    > why not just use


    really i use:

    #define BIT(bit) (1UL<<(bit))
    #define BIT0 BIT(0)
    ....

    i agree that this is only cosmetic .....

    i wanted:
    - avoid repeat "i" in #define BITi BIT(i)
    - use BITi that "looks" as a constant and not BIT(i) that locks as a
    function


    On Jun 18, 6:37 pm, CryptiqueGuy <> wrote:

    > You can write the code, for generating such sequence #define
    > BIT0..etc. and print it in the required .c file.
    > This is the only possible solution I envisage.


    yes, but i was looking for a language or "academic" solution, not for
    avoid writting, at the end, there are only 32 lines to write (for my
    case)

    tks again to all!
    rhXX, Jun 18, 2007
    #4
  5. On Mon, 18 Jun 2007 16:13:41 +0000, rhXX wrote:

    > hi all,
    >
    > i want to define a group of consts for bits
    >
    > #define BIT0 (1 << 0)
    > #define BIT1 (1 << 1)
    > ...
    > #define BITi (1 << i)
    >
    > is it a way to do it more elegant? or at least to put "i" as parameter
    > of other macro?
    >
    > how i can do some like this???
    >
    > #define BITX(i) #define BIT##i (1 << i)
    >
    >
    > tks in advance

    Do you need macros? I'd use:
    enum
    { BIT0=0x0001
    , BIT1=0x0002
    /* etc */
    };

    Duncan
    Duncan Muirhead, Jun 18, 2007
    #5
  6. rhXX

    Barry Guest

    "rhXX" <> wrote in message
    news:...
    > hi all,
    >
    > i want to define a group of consts for bits
    >
    > #define BIT0 (1 << 0)
    > #define BIT1 (1 << 1)
    > ...
    > #define BITi (1 << i)
    >
    > is it a way to do it more elegant? or at least to put "i" as parameter
    > of other macro?


    If you are new to C, never let yourself get caught up in "elegance."
    Concentrate on correctness and readability. BITi is prone to
    undefined behavior.

    Often folks new to C, read really bad code that makes poor use
    of macros. Then, they assume it is the way it should be done.

    Whether this simple macro is an improvement on your code is
    most likely subjective.

    >
    > how i can do some like this???
    >
    > #define BITX(i) #define BIT##i (1 << i)
    >
    >
    > tks in advance
    >
    Barry, Jun 19, 2007
    #6
  7. rhXX

    rhXX Guest

    tks to all for ideas and comments!
    rhXX, Jun 19, 2007
    #7
  8. rhXX

    Thad Smith Guest

    rhXX wrote:

    > really i use:
    >
    > #define BIT(bit) (1UL<<(bit))
    > #define BIT0 BIT(0)
    > ...
    >
    > i agree that this is only cosmetic .....
    >
    > i wanted:
    > - avoid repeat "i" in #define BITi BIT(i)
    > - use BITi that "looks" as a constant and not BIT(i) that locks as a
    > function


    Looks like a constant? Constant expressions work as well as constants
    and have the advantage of showing you how the constant is generated.

    --
    Thad
    Thad Smith, Jun 19, 2007
    #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. Replies:
    80
    Views:
    2,422
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    441
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    495
  4. Michael T. Babcock

    Re: Explanation of macros; Haskell macros

    Michael T. Babcock, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    516
    Michael T. Babcock
    Nov 3, 2003
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    492
    S.Tobias
    Jul 24, 2004
Loading...

Share This Page