how to define a macro of two tokens in cpp

You must first expand the macro inside. Ie. do another pass. Your code may look like this:

#define SET(x,y)      do{ (x) |=  (1u << (y)); }while(0)
#define CLEAR(x,y)    do{ (x) &= ~(1u << (y)); }while(0)

#define HIGH(a)       SET(a) // another empty pass, just forward
// the `a` is expanded and the second `SET` takes two arguments
// or better, but not fully compliant:
// #define HIGH(...)  SET(__VA_ARGS__)

#define CLOCK         PORTB, 7
#define CLOCK_HIGH()  HIGH(CLOCK)

int main() {
    int PORTB;
    CLOCK_HIGH();
}

As a good measure research about macro pitfalls and research good practices when writing macros..

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top