[英]C macro using two macros
I want to make a macro to utilize two macros我想制作一个宏来利用两个宏
#define BUZZER_ON func_buzz(1);\
flag_buzzer_on = 1;\
#define BUZZER_OFF func_buzz(0);\
flag_buzzer_on = 0;\
#define BUZZER_TOGGLE ((flag_buzzer_on == 1) ? BUZZER_OFF : BUZZER_ON ) ;
where func_buzz is function to turn buzzer on or off depending on value passed其中 func_buzz 是 function,根据传递的值打开或关闭蜂鸣器
BUZZER ON and BUZZER OFF MACRO is working correctly BUZZER ON 和 BUZZER OFF MACRO 工作正常
but when i use MACRO BUZZER_TOGGLE i get error但是当我使用 MACRO BUZZER_TOGGLE 时出现错误
expression expected: or ) before;预期表达:或)之前;
How to write MACRO BUZZER_TOGGLE如何编写 MACRO BUZZER_TOGGLE
remember that in your case you can think of macro expansion simplified as text replacement although that's not quite correct as @Eric Postpischil has correcty stated in his comment.请记住,在您的情况下,您可以将宏扩展简化为文本替换,尽管这并不完全正确,因为@Eric Postpischil 在他的评论中已正确说明。
In your case the line在你的情况下
BUZZER_TOGGLE;
is expanded to扩展为
((flag_buzzer_on == 1) ? func_buzz(0); flag_buzzer_on = 0; : func_buzz(1); flag_buzzer_on = 1;) ;;
(assuming the second #define BUZZER_ON
in the question is a typo for BUZZER_OFF
). (假设问题中的第二个#define BUZZER_ON
是BUZZER_OFF
的拼写错误)。
You can see that this is no valid statement.您可以看到这不是有效的陈述。 You could make it valid if you defined如果你定义了,你可以让它有效
#define BUZZER_ON (func_buzz(1), flag_buzzer_on = 1)
and BUZZER_OFF
accordingly和BUZZER_OFF
but maybe it's easier just to use simple functions instead of macros.但也许只使用简单的函数而不是宏会更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.