generating macros with another macro

R

rhXX

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
 
E

Eric Sosman

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?
 
C

CryptiqueGuy

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

rhXX

tks eric and CryptiqueGuy

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


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!
 
D

Duncan Muirhead

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
 
B

Barry

rhXX said:
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.
 
T

Thad Smith

rhXX said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top