简体   繁体   English

c评估顺序

[英]c evaluation order

let's assume I have the followin code 假设我有以下代码

#define CHECK(result) do{                         \
                          if(result == 0)         \
                                 return false;    \
                           } while(0)


int sum(int a, int b){

    return (a + b);
}

int main(){
   int a = b = 0;
   CHECK(sum(a + b));
   reutnr 0;
}

my question is what is an order of evaluation in C, I mean: 我的问题是在C中求值的顺序是什么,我的意思是:

result = sum(a, b) 
//and only after checking              
if(result == 0)         
   return false;    

or 要么

if(sum(a + b) == 0)         
   return false; 

thanks in advance 提前致谢

The macro substitution will be done before the actual compiler even sees the code, so the code that is compiled will read 宏替换将在实际编译器看到代码之前完成,因此编译后的代码将读取

int main(){
  int a = b = 0;
  do {
    if(sum(a+b) == 0)
     return false;
  } while(0);
  reutnr 0;
}

There will never be a variable called result . 永远不会有一个名为result的变量。

Also note that C does not have a keyword called false . 另请注意,C没有名为false的关键字。

C macros are plain text substitutions. C宏是纯文本替换。 The compiler will see exactly: 编译器将完全看到:

do {
  if(sum(a + b) == 0)
    return false;
} while(0);

Your macro does not "generate" a result variable. 您的宏不会“生成” result变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM