samedi 30 avril 2016

C++ Addition within a Macro Function Argument

I have this snippet of C++ code from an exam. We are just suppose to write out what the output of the program is, I assumed the output would be '20', but the output is '10'. Why is this?

#define func(x) (x*x)-x

int i=3;

int main() {
    cout  << func(i+2) << endl;
}

If I put the i+2 in brackets like so:

cout  << func( (i+2) ) << endl; 

The output is '20' as assumed it would be.

How does C++ process this code that makes it return 10 not 20?

Aucun commentaire:

Enregistrer un commentaire