0
   #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

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

2 Answers2

7

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)
mch
  • 9,424
  • 2
  • 28
  • 42
4
 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)
ameyCU
  • 16,489
  • 2
  • 26
  • 41