如果条件为C宏 [英] C-Macro in if-Condition

查看:76
本文介绍了如果条件为C宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个简单的问题.

quite a simple problem I have here.

我有一个宏用于全局变量,该宏在我的标头中定义如下:

I have a little Macro for a global Variable which is defined in my Header like this:

extern bool uart_message_received;
#define get_uart_message_rec() uart_message_received;

在我的C文件中,我想像这样访问文件:

In my C-File I want to access the file like this:

 bool uart_message_received = 0;
 void foo(void)
 {
       bool test;
       test = get_uart_message_rec();  // Works fine

       if(get_uart_message_rec()==0)    // Doesn't work
       {
          //...
       } 
 }

我有点困惑为什么if中的条件不起作用.我是在做错什么,还是违反了某些C指令?

I am a little confused why the condition in the if is not working. Am I doing something wrong, or am I violating some C directives?

推荐答案

#define get_uart_message_rec() uart_message_received
                                                 // ^ no semicolon

宏替换将按原样替换文本,包括您所在的情况下的;.在if情况下会导致语法错误.

Macro replacement will substitute the text as is, including the ; in your case. Which will lead to syntax errors in the if case.

这篇关于如果条件为C宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆