Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C program:output seems confusing

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,in my opinion,out should be 16. but OUTPUT: 7 Anyone please tell me how? Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An important C programing lesson: macros aren't functions! They are just expanded as text, and then the text is compiled as part of the normal code. This macro expands to the text "x*x" for a given value of "x"; here "x" is "p+1", so we get "p+1*p+1", which (just as in Java) evaluates to p + (1 * p) + 1 = 7 (since p is 3.)
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote: here "x" is "p+1", so we get "p+1*p+1",evaluates to p + (1 * p) + 1 = 7

Yeah,Nice.
Thanks. That was really simple.
idk where my mind is these days.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change PROD(p+1) to PROD((p+1)).

PROD is an inappropriate name for your macro; you should have used SQUARE or similar.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or better still, #define SQUARE(x) ((x)*(x))
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . or better still, #define SQUARE(x) ((x)*(x))


That's the classic dodge, although then the follow-on question is "Why does this give 12 rather than 9?"



 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
Campbell Ritchie wrote: . . . or better still, #define SQUARE(x) ((x)*(x))

That's the classic dodge, although then the follow-on question is "Why does this give 12 rather than 9?"

Ok if you seriously want to know how 12 comes,i will tell you

Thanks for the guidance.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
Campbell Ritchie wrote: . . . or better still, #define SQUARE(x) ((x)*(x))


That's the classic dodge, although then the follow-on question is "Why does this give 12 rather than 9?"



What about SQUARE(x++)?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What about SQUARE(x++)?

First of all,please be serious and stop laughing,we are having serious discussion here.
Above one is still tricky, but never mind here we go

 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun Srivastava wrote:. . . serious discussion here. . . .
You jest


I challenge you to find a C implementation which returns 12 from SQUARE(x++). It shouldn't take you more than an hour.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You jest
find a C implementation which returns 12 from SQUARE(x++).

Phew. You are G(enius).
I have tried more than an hour ,but didn't able to crack that joke oops sorry that equation.
What is the answer?please tell me.
Answer again confusing to below code

Answer again confusing: 9 49 7
I was thinking j=3*4=12, k=6*7=42 , i=7
What is happening?did i miss something?
(x*x)=((x)*(x)) same here.it doesn't matter.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to try different implementations, as I said. The behaviour of i++ is undefined in C, and if you try enough different C compilers, you will find some which will return 6 from that code, and some which will return 12.

SQUARE(x++) will expand to (x++ * x++) You are obviously getting 3 returned the first time, and squaring that to 9. By the end of the first line x is 5, and by the end of the second line it is 7. So you are obviously the final value of ++x twice, and the initial value of x++ twice in the first line. That is a different behaviour from what I expected. I tried your code on "gcc" and got 9 49 7 as output.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's undefined because the standard says so. Put it through a decent compiler like gcc - Wall -std=c99 to get warning: operation on 'i' may be undefined. The fact that it compiles on a particular implementation (the ancient Turbo C by the looks of it) is not relevant. You can do it, just be aware that the results may not always be what you expect.

Campbell Ritchie wrote:I tried your code on "gcc" and got 9 49 7 as output.

Hi sir, did you also get any warning while compiling this code on gcc?
j=9
k=49
l=81
m=81
I saw the pattern here.
for post increment i++ :it gives square of i(first value)
For pre increment ++i :it gives square of i(second value)
Also if line 1 changes to ((x)+(x))
j=6
k=14
l=18
m=18
It follows the same pattern.
Does this make any sense? can i use this technique for solving pseudo codes output?
Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun Srivastava wrote: . . . Hi sir, did you also get any warning while compiling this code on gcc?
No, but I didn't use -Wall
. . . It follows the same pattern.
Does this make any sense?
Yes
can i use this technique for solving pseudo codes output?
Probably not.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
I challenge you to find a C implementation which returns 12 from SQUARE(x++).

Btw what was that implementation?
I didn't get through.
Please declare the answer.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't remember. Sorry. I used to have three C/C++ compilers running on a Windows box; one was Dev-C++, which is a nice tool which returns 4 from. . . so it wasn't that. (If you want a C compiler for Windows, I think Dev-C++ is a good bet).

There are lots of hits on this Google search for "c compiler for windows"; there was one maybe Miracle-C which returned 5 from the above code snippet.
Sorry, I can't remember any more
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, now am using Dev-C++ compiler, it is much more better than old turbo C and friendly too.
Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Dev-C++ is a nice application, isn't it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic