geek
geek

Reputation: 2847

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:

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

or

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

thanks in advance

Upvotes: 1

Views: 250

Answers (2)

unwind
unwind

Reputation: 399703

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.

Upvotes: 1

Mat
Mat

Reputation: 206669

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.

Upvotes: 0

Related Questions