943,852 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1022
  • C RSS
Sep 28th, 2007
0

wht will be the o/p?

Expand Post »
#include <stdio.h>
int main()
{
int i=5;
printf("%d %d %d %d %d", --i,i--,++i,--i,i);

return 0;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhishekgahoi is offline Offline
2 posts
since Sep 2007
Sep 28th, 2007
1

Re: wht will be the o/p?

#include <stdio.h>
int main()
{
int i=5;
printf("%d %d %d %d %d", --i,i--,++i,--i,i);

return 0;
}
Undefined Behaviour.
Last edited by Aia; Sep 28th, 2007 at 6:32 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Sep 28th, 2007
0

Re: wht will be the o/p?

it is easy
i is 5
then -- i which means (i) is 4
then i-- which will appear 4 then i will be 3 then
++i which means increase by one so (i) is 4 then
-- i becomes i is 3, then print last (i) which is 3

the result is 4 4 4 3 3
please reply if I am mistaken
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
alsoumhi is offline Offline
66 posts
since Aug 2007
Sep 28th, 2007
0

Re: wht will be the o/p?

> please reply if I am mistaken
You are mistaken.
Read the link Aia posted.

Trying to rationalise what you expect to happen, or merely posting what your current compiler gives you is no way to go.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 30th, 2007
0

Re: wht will be the o/p?

Thank you I learnt somthing from you
but I was thinking the program will run as
this following:

#include<stdio.h>
int main()
{
int i=5;
printf("%d",--i);
printf("%d",i--);
printf("%d",++i);
printf("%d",--i);
printf("%d",i);

return 0;
}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
alsoumhi is offline Offline
66 posts
since Aug 2007
Sep 30th, 2007
0

Re: wht will be the o/p?

That would be a completely different program, which is fine.

Use that FAQ to also look up "sequence points". You'll see that each side effect is separated by a sequence point, so there is no issue about the order of things and your program is well defined.

But to say it always has the same output as the one-liner in the original post would be wrong.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 30th, 2007
0

Re: wht will be the o/p?

Did you run the program before asking?
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Sep 30th, 2007
0

Re: wht will be the o/p?

Click to Expand / Collapse  Quote originally posted by twomers ...
Did you run the program before asking?
Running a given program is not a garantee that is correct, legal code.
Last edited by Aia; Sep 30th, 2007 at 1:45 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: time & date in c
Next Thread in C Forum Timeline: changing values of a struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC