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. Advertisements

  2. rhXX

    Eric Sosman Guest

    rhXX wrote On 06/18/07 12:13,:
    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. Advertisements

  3. rhXX

    CryptiqueGuy Guest

    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.
     
    CryptiqueGuy, Jun 18, 2007
    #3
  4. rhXX

    rhXX Guest

    tks eric and CryptiqueGuy

    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


    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. Do you need macros? I'd use:
    enum
    { BIT0=0x0001
    , BIT1=0x0002
    /* etc */
    };

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

    Barry Guest

    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.
     
    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

    Looks like a constant? Constant expressions work as well as constants
    and have the advantage of showing you how the constant is generated.
     
    Thad Smith, Jun 19, 2007
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.