0

In my last interview i found this tricky question. after these two lines what will be the value of i.

int i=c;
//c is a constant
i=i++;

where c is a constant(where c is initialised before). please give me step by step answer rather than one word answer.

Shamseer K
  • 4,964
  • 2
  • 25
  • 43
  • 1
    If its C#, then why Java is tagged ? – P0W Feb 14 '15 at 06:09
  • Did you even bother to try to run the code? The question is addressed by section 7.6.9 of the C# 5.0 specification, but if all you want to know is what the code will do, it's silly to post a question here. You can just run it yourself. – Peter Duniho Feb 14 '15 at 06:17
  • possible duplicate of [What's the difference between X = X++; vs X++;?](http://stackoverflow.com/questions/226002/whats-the-difference-between-x-x-vs-x) – Peter Duniho Feb 14 '15 at 06:19
  • @PeterDuniho more specifically i need explanation – Shamseer K Feb 14 '15 at 06:22
  • You need a better explanation than already exists in the duplicate question? – Peter Duniho Feb 14 '15 at 06:28

1 Answers1

3

finally value of i remains same as c.

Explanation:- when i is assigned with i++;

step 1. first i++ return value c(but not assigned to left-hand operand i).

step 2. then i++ increments value of i to c+1.

step 3. i is assigned with value c that is returned in step1.

In effect value of i remains same but some where along the execution it was c+1, But finally it is assigned with value c.

samurai
  • 68
  • 7