开发者

c evaluation order

开发者 https://www.devze.com 2023-04-08 16:24 出处:网络
let\'s assume I have the followin code #define CHECK(result) do{\\ if(result == 0)\\ return false;\\ } while(0)

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:

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

or

if(sum(a + b) == 0)         
   ret开发者_JS百科urn 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.

Also note that C does not have a keyword called false.


C macros are plain text substitutions. The compiler will see exactly:

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

Your macro does not "generate" a result variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消