-2

I am running the code below:

#include<stdio.h>
#define x 5+2

int main() {

    int p;
    p = x*x*x;
    printf("%d",p);

}

I was expecting 343 as the output, but the answer is 27. Why so? I am unable to figure it out why it's happening. Please help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rishu
  • 77
  • 1
  • 1
  • 10

4 Answers4

5

This is why it's 27 and not 343: . before -! So look at this:

#include<stdio.h>
#define x 5+2

int main() {

    int p;
    p = x*x*x;  //5 + 2*5 + 2*5 + 2 -> 5 + (2*5) + (2*5) + 2 -> 5 + 10 + 10 + 2 = 27

    printf("%d",p);

    return 0;

}

You would have to change it to this:

p = (x)*(x)*(x);

Or define it with this line:

#define x (5+2)

So that the result is 343

As a reference take a look here to the C Operator precedene: http://en.cppreference.com/w/c/language/operator_precedence

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • better change to `#define x (5+2)`. Then using `x` anywhere would lead to the expected results – Ingo Leonhardt Jan 07 '15 at 13:21
  • Got it :) Thanks for answering :) Can you cite any resource where I could learn about #define and it's functioning? – Rishu Jan 07 '15 at 13:23
  • @Rishu You're welcome! It's not a define problem, it the precedence of the operators, i put a link in my answer so you can check it out! – Rizier123 Jan 07 '15 at 13:25
  • @Rizier123 : Thanks ! Yes, I understood its about precedence.I have no understanding about #define either, could you cite any source if possible.Will appreciate :) Thanks – Rishu Jan 07 '15 at 13:29
  • @Rishu For sure check this out: http://www.cplusplus.com/doc/tutorial/preprocessor/ It's about the C preprocessor Directives (including `#define`) – Rizier123 Jan 07 '15 at 13:31
4
p = 5+2*5+2*5+2

ans: 27

use:

#define x (5+2)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Md Mohsin
  • 1,568
  • 1
  • 18
  • 28
2

x * x * x with #define x 5 + 2 will expand to

p = 5 + 2 * 5 + 2 * 5 + 2

and hence you will have 5 + 10 + 10 + 2 = 27, you have to add parentheses

#define x (5 + 2)

and then it will expand to

p = (5 + 2) * (5 + 2) * (5 + 2) // -> 7 * 7 * 7 -> 343

#define does not define variables, it defines macros.

You can read about the c preprocessor for more information.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • No good reason for downvoting, what is wrong with this answer? – Iharob Al Asimi Jan 07 '15 at 13:23
  • I didn't downvote pal! I haven't much reputation to downvote even. Though I am wondering as a beginner ,was this question really poor one that I got 2 downvotes :) Anyways thanks for answering :) – Rishu Jan 07 '15 at 13:26
  • @Rishu it wasn't you for sure, but apparently some one just did it becase thy hace so much reputation they want to get rid of it :), It's just that if there is something wrong in this answer I'd like to know. – Iharob Al Asimi Jan 07 '15 at 13:28
  • @Rishu, you need to read about the c preprocessor, see my answer, now i added a link. – Iharob Al Asimi Jan 07 '15 at 13:30
0

After macro expansion

p=x*x*x;  

will be

p = 5+2*5+2*5+2;
haccks
  • 104,019
  • 25
  • 176
  • 264