Hi,
I have the following code:
#define SET_BIT(reg,bit_no) reg |= (1<<bit_no)
#define TRANSMITTER_ENABLE_BIT SCI2C2, 7
void main(void){
SET_BIT(TRANSMITTER_ENABLE_BIT);
}
With this code I want to put two arguments SCI2C2 and 7 into SET_BIT() macro. But the compiler says that there is only one argument detected instead of two in the line SET_BIT(TRANSMITTER_ENABLE_BIT);.
Can someone please tell me how to pass two arguments to macro SET_BIT() ?
Best regards,
Florijan
Solved! Go to Solution.
Hi Florijan,
I don't think you can do so since function-like macros are evaluated from outside in, as explained here: Macro Call as Macro Argument . Hence SET_BIT is evaluated first, the preprocessor sees it requires two arguments, but only one is found. It ends up with error without evaluating the inner macro (TRANSMITTER_ENABLE_BIT).
I see only two solutions:
1:
#define SET_BIT(reg,bit_no) reg |= (1<<bit_no)
#define TRANSMITTER_ENABLE_REG SCI2C2
#define TRANSMITTER_ENABLE_BIT 7
void main(void){
SET_BIT(TRANSMITTER_ENABLE_REG, TRANSMITTER_ENABLE_BIT);
}
2:
#define SET_BIT(reg,bit_no) reg |= (1<<bit_no)
#define TRANSMITTER_ENABLE SET_BIT(SCI2C2,7)
void main(void){
TRANSMITTER_ENABLE();
}
Hi Florijan,
I don't think you can do so since function-like macros are evaluated from outside in, as explained here: Macro Call as Macro Argument . Hence SET_BIT is evaluated first, the preprocessor sees it requires two arguments, but only one is found. It ends up with error without evaluating the inner macro (TRANSMITTER_ENABLE_BIT).
I see only two solutions:
1:
#define SET_BIT(reg,bit_no) reg |= (1<<bit_no)
#define TRANSMITTER_ENABLE_REG SCI2C2
#define TRANSMITTER_ENABLE_BIT 7
void main(void){
SET_BIT(TRANSMITTER_ENABLE_REG, TRANSMITTER_ENABLE_BIT);
}
2:
#define SET_BIT(reg,bit_no) reg |= (1<<bit_no)
#define TRANSMITTER_ENABLE SET_BIT(SCI2C2,7)
void main(void){
TRANSMITTER_ENABLE();
}
Hi,
thank you all for your help and time.
Erich thanks for info.
Petr, I will change the code like you suggested. Carlh, the brackets didn't help but thanks anyway for your time :smileyhappy:.
Best regards,
Florijan
While with C preprocessing things are 'textually' replaced, this does not apply to the macro argument expansion. You cannot pass an argument which then is expanded in to a list of argument (what you have in your code).
You need to supply the right number of arguments. What you could do is pass an 'empty' argument, as
SET_BIT(TRANSMITTER_ENABLE_BIT,);
(note the comma, for an 'empty' argument.
I think you need to rewrite your code as such that it is using two arguments.
Unless someone else has a different solution?
Wouldn't brackets help here?
#define TRANSMITTER_ENABLE_BIT (SCI2C2, 7)