programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# C program:output seems confusing

Arjun Srivastava
Ranch Hand
Posts: 432

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

Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
• 2
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
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
Marshal
Posts: 56197
171
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
Marshal
Posts: 56197
171
. . . or better still, #define SQUARE(x) ((x)*(x))

Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
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
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
Marshal
Posts: 56197
171
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?"

Arjun Srivastava
Ranch Hand
Posts: 432

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
Marshal
Posts: 56197
171
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
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.
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
Marshal
Posts: 56197
171
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
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
Marshal
Posts: 56197
171
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
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.

Campbell Ritchie
Marshal
Posts: 56197
171
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
Yeah, now am using Dev-C++ compiler, it is much more better than old turbo C and friendly too.
Thanks.

Campbell Ritchie
Marshal
Posts: 56197
171
You're welcome. Dev-C++ is a nice application, isn't it.