wht will be the o/p?

Reply

Join Date: Sep 2007
Posts: 2
Reputation: abhishekgahoi is an unknown quantity at this point 
Solved Threads: 0
abhishekgahoi abhishekgahoi is offline Offline
Newbie Poster

wht will be the o/p?

 
0
  #1
Sep 28th, 2007
#include <stdio.h>
int main()
{
int i=5;
printf("%d %d %d %d %d", --i,i--,++i,--i,i);

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,092
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 185
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: wht will be the o/p?

 
1
  #2
Sep 28th, 2007
Originally Posted by abhishekgahoi View Post
#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 5:32 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan stating how a liberal's mind works.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 63
Reputation: alsoumhi is an unknown quantity at this point 
Solved Threads: 0
alsoumhi alsoumhi is offline Offline
Junior Poster in Training

Re: wht will be the o/p?

 
0
  #3
Sep 28th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,601
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 853
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: wht will be the o/p?

 
0
  #4
Sep 28th, 2007
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 63
Reputation: alsoumhi is an unknown quantity at this point 
Solved Threads: 0
alsoumhi alsoumhi is offline Offline
Junior Poster in Training

Re: wht will be the o/p?

 
0
  #5
Sep 30th, 2007
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;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,601
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 853
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: wht will be the o/p?

 
0
  #6
Sep 30th, 2007
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,873
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: wht will be the o/p?

 
0
  #7
Sep 30th, 2007
Did you run the program before asking?
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,092
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 185
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: wht will be the o/p?

 
0
  #8
Sep 30th, 2007
Originally Posted by twomers View Post
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 12:45 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan stating how a liberal's mind works.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 950 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC