C 什么';这个宏怎么了?

C 什么';这个宏怎么了?,c,macros,C,Macros,我面临一个宏问题,我不知道为什么 以下是宏: #define WAIT(condition, max_time) \ do { \ int int_loop_wait=0; \ while(1) \ {

我面临一个宏问题,我不知道为什么

以下是宏:

#define WAIT(condition, max_time)               \
   do {                                         \
      int int_loop_wait=0;                      \
      while(1)                                  \    
      {                                         \           
        if(condition) { break; }                \
        sleep(1);                               \
        if(int_loop_wait>=max_time) { break; }  \
        int_loop_wait++;                        \
      }                                         \
    } while(0)                                  \
我弄错了

如果(条件){break;},则需要声明“line”


有人理解这个错误吗

从最后一行删除
\

我的意思是改变这条线

 } while(0)                                                     \

并删除
\

\
后面有一些包含空格的行,如:

while(1)                                  \    
      {                                         \           

罪魁祸首是
\
后面的空白。移除它们将解决问题

如果行以
\
结尾,但不以空格
或任何其他字符结尾,则宏定义将继续

#define WAIT(condition, max_time)                                  \
   do {                                                            \
      int int_loop_wait=0;                                         \
      while(1){                                                    \
        if(condition) { break; }                                   \
        sleep(1);                                                  \
    if(int_loop_wait>=max_time) { break; }                         \
      }                                                            \
    } while(0)  

问题是反斜杠后面跟一个空格一起被认为是一个转义序列,它实际上抵消了反斜杠。Visual C++ 10甚至发出<代码>错误C2017:非法逃逸序列< /代码>。

代码段中的某些行(例如带有
而(1)
的行)在反斜杠后包含一个或多个空格。一旦反斜杠被视为转义序列并被编译器删除,宏定义将在该行被截断,剩余的代码将被编译,就好像它不属于宏定义一样

#define WAIT(condition, max_time)               \
   do {                                         \
      int int_loop_wait=0;                      \
      while(1)                                  \    <<<<<WHITESPACES
      {                                         \<<<this line doesn't belong to macro
        if(condition) { break; }                \<<<and neither does this 
        sleep(1);                               \
        if(int_loop_wait>=max_time) { break; }  \
        int_loop_wait++;                        \
      }                                         \
    } while(0)                                  \
#定义等待(条件、最长时间)\
做{\
int_loop_wait=0\

虽然(1)\n您没有在循环内更改
int\u loop\u wait
max\u time
没有效果。是的!我现在就更改它。我让您更新@Krishnabhadra它是一个宏,所以我不想要分号(它将添加到代码中)@Krishnabhadra,如果你想写
等待(条件,最长时间)
的话,就不需要
,宏调用在哪里?知道为什么空格会导致问题吗?空格会给你带来一些麻烦。比如c.c:4:49:警告:反斜杠和换行符用空格分隔[默认启用]c.c:5:49:警告:反斜杠和换行符以空格分隔[默认启用]
#define WAIT(condition, max_time)               \
   do {                                         \
      int int_loop_wait=0;                      \
      while(1)                                  \    <<<<<WHITESPACES
      {                                         \<<<this line doesn't belong to macro
        if(condition) { break; }                \<<<and neither does this 
        sleep(1);                               \
        if(int_loop_wait>=max_time) { break; }  \
        int_loop_wait++;                        \
      }                                         \
    } while(0)                                  \