*
The moose likes C / C++ and the fly likes C program:output seems confusing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "C program:output seems confusing" Watch "C program:output seems confusing" New topic
Author

C program:output seems confusing

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


Hi,in my opinion,out should be 16. but OUTPUT: 7 Anyone please tell me how? Thanks.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.)

[Jess in Action][AskingGoodQuestions]
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Oct 13, 2005
Posts: 38107
    
  22
. . . or better still, #define SQUARE(x) ((x)*(x))
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jun 23, 2010
Posts: 432

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jun 23, 2010
Posts: 432

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jun 23, 2010
Posts: 432

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jun 23, 2010
Posts: 432

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jun 23, 2010
Posts: 432

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jun 23, 2010
Posts: 432

Yeah, now am using Dev-C++ compiler, it is much more better than old turbo C and friendly too.
Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
You're welcome. Dev-C++ is a nice application, isn't it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: C program:output seems confusing
 
Similar Threads
Project Euler problems with Scala
Operators
Removing a href tags using XSL
extracting image binary data from file
what does this mean?