#include <iostream>
using namespace std;
# define p 5+3
int main() {
//code
int i=p*p;
cout<<i;
return 0;
}
expected output:64 actual output:23 howwW?? I dont understand
#include <iostream>
using namespace std;
# define p 5+3
int main() {
//code
int i=p*p;
cout<<i;
return 0;
}
expected output:64 actual output:23 howwW?? I dont understand
p
will be replaced by 5+3
. So the line int i=p*p;
is int i=5+3*5+3;
. *
has an higher rank than +
, so the result is 23
.
You need to paranthese your define:
#define p (5+3)
int i=p*p;
p*p
is replaced as 5+3*5+3
,thus gives 23 (5+15+3=23)
.As you know *
will be evaluated before +
.
Define macro as follows -
#define p (5+3)