GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  OOPS
Go To First  |  Previous Question  |  Next Question 
 OOPS  |  Question 221 of 255    Print  
could any one explan me what happens in this program.

main(){
int i=10;
i=(++i)/(i++);
printf("i=%d",i);
}

  
Total Answers and Comments: 11 Last Update: March 09, 2008     Asked by: vaasu 
  
 Sponsored Links



 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
May 25, 2006 00:39:21   #1  
Umme Zakira        

RE: could any one explan me what happens in this progr...
the value of i in the given program is 2
 
Is this answer useful? Yes | No
May 25, 2006 06:34:06   #2  
rajan        

RE: could any one explan me what happens in this progr...
In c it will do some right to left approach so the value of i=1.2
 
Is this answer useful? Yes | No
May 25, 2006 13:56:04   #3  
srinivas        

RE: could any one explan me what happens in this progr...
i know that the output is 2.but i want how the complier gives it.
 
Is this answer useful? Yes | No
May 29, 2006 05:55:13   #4  
Malathi        

RE: could any one explan me what happens in this progr...

Here, ++i is previx expression -- i will be incrementd before the statement and i++ is postfix --> I will be incremented after the statement .

i=(++i)/(i++);  --- 1. ++i --> i will be incremented first.-> 11

                          2. 11/i ==11/10 = 1.1

                          3. (i++) = 1.1++ = 2.1

printf("i=%d",i);  --> %d id for integers, therefore 2.1 is trucated to 2.

Bye


 
Is this answer useful? Yes | No
May 31, 2006 03:18:52   #5  
AS        

RE: could any one explan me what happens in this progr...

Reason is quiet simple

i=(++i)/(i++); it will  resolve

like 

i=11/10 which give 1 being int

now post increment works and increment new val of i thus giving 2.

Thanks


 


 
Is this answer useful? Yes | No
June 21, 2006 15:06:58   #6  
amit        

RE: could any one explan me what happens in this progr...
The output of this program will be undefined. In C and C++, if you read a variable twice in an expression where you also write it, the result is undefined.for further details : http://www.research.att.com/~bs/bs_faq2.html#evaluation-order
 
Is this answer useful? Yes | No
June 28, 2006 07:51:54   #7  
swapna s        

RE: could any one explan me what happens in this progr...

Solution:-

      i = 10;

      i = (++i)/(i++)

      ++i is preincrement the value of i before executing the statement. 

      i++ is post increment the value of i after executing the statement     

Here ++i = 11

      i = (++i) / (i++);  // 11/11 = 1 

           After executing the above statement post increment operator (ie i++)

           is excuted. Becaz of that i's value is incremented to 2. 

      


 
Is this answer useful? Yes | No
July 03, 2006 01:41:06   #8  
sami ahmed        

RE: could any one explan me what happens in this progr...

i agree with swapna s

but the output will b 1..


 
Is this answer useful? Yes | No
January 26, 2008 18:24:43   #9  
zdmytriv Member Since: January 2008   Contribution: 8    

RE: could any one explan me what happens in this program. main(){ int i=10; i=(++i)/(i++); printf("i=%d",i); }
swapna was right

output: 2

 
Is this answer useful? Yes | No
February 26, 2008 01:13:14   #10  
chethan Member Since: November 2005   Contribution: 3    

RE: could any one explan me what happens in this program. main(){ int i=10; i=(++i)/(i++); printf("i=%d",i); }
1. i ++ will get execute so the value will be 10 . it wont increment.
2. ++i will be incremented to 11
3. 11/10 will result to 1
4.i=1 will get execute.
5. keep in mind still increment is not done.. this is done after exectuing , or ;
6. after executing i=1  ';' will be executed
7. i++ will get incremented to 2

so the answer is 2.

 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links



 
Sponsored Links

 
Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape