in Programming in C edited by
8,479 views
13 votes
13 votes

If n has  3, then the statement a[++n]=n++;

  1. assigns 3 to a[5]
  2. assigns 4 to a[5]
  3. assigns 4 to a[4]
  4. what is assigned is compiler dependent
in Programming in C edited by
8.5k views

8 Answers

15 votes
15 votes
Best answer

It is compiler dependent

Reference :Undefined behaviour

selected by

4 Comments

@manoj sir, codepad give 6 

                 tutorial point give 5

                  ideaone give 5.         why?

0
0

That is what is undefined behavior of C.

i = i++

which modify the same value twice which needn't be allowed.

Similarly here

a[i] = i++

which modify i and use it along the way.

Its all depends on compiler what value is will be assigned .

I think it should be clear . I dont know much about undefined stuffs.

0
0
Final value of i is 6
0
0
5 votes
5 votes
It is an undefined behaviour since between two sequence points which is here ; at the end of this statement and the other ; which would be present above this statement in the actual code we cannot modify the value of a variable more than once therefore here u r trying to modify the value of n two times so it depends on compiler wither it will evaluate ++n inside array subscript or assign ++n to array index .
2 votes
2 votes

Ans is D.It depends on the compiler.Here n is updated twice before the next sequence point is reached.ref:https://en.wikipedia.org/wiki/Sequence_point

2 Comments

So whenever there are 2 or more modifications on the same variable, it is always compiler dependent?
0
0
No. It is about how and in which order prefix and postfix operator perform its function
0
0
0 votes
0 votes
Answer : option C- assigns 4 to a[4]
Answer:

Related questions