Could any one explan me what happens in this program. main(){ int i=10; i=(++i)/(i++); printf("i=%d",i); }

Questions by vaasu

Showing Answers 1 - 12 of 12 Answers

Umme Zakira

  • May 25th, 2006
 

the value of i in the given program is 2

  Was this answer useful?  Yes

rajan

  • May 25th, 2006
 

In c it will do some right to left approach so the value of i=1.2

  Was this answer useful?  Yes

srinivas

  • May 25th, 2006
 

i know that the output is 2.but i want how the complier gives it.

  Was this answer useful?  Yes

Malathi

  • May 29th, 2006
 

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

  Was this answer useful?  Yes

AS

  • May 31st, 2006
 

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


 

  Was this answer useful?  Yes

amit

  • Jun 21st, 2006
 

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

  Was this answer useful?  Yes

swapna s

  • Jun 28th, 2006
 

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. 

      

  Was this answer useful?  Yes

sami ahmed

  • Jul 3rd, 2006
 

i agree with swapna s

but the output will b 1..

  Was this answer useful?  Yes

chethan

  • Feb 26th, 2008
 

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.

  Was this answer useful?  Yes

KishoreKNP

  • Dec 2nd, 2009
 

The answer is 2, because  the statement
i = (++i) / (i++)

will be processed from left to right
i = (++10) / (10++)

which means

i = 11/10++

Here still ++ operator is not executed

i = 1++; // 1 because it is an integer operation
i = 2;

if you change
i = (++i) / (++i) you will see 1

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions