The Gateway to Computer Science Excellence
+8 votes
3.4k views

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 by (83 points)
reopened by | 3.4k views

5 Answers

+13 votes
Best answer

It is compiler dependent

Reference :Undefined behaviour

by Boss (38.7k points)
selected by
0

@manjok sir : in the precedence table in let us c given increment/decrements have right to left associativity . so according to that point the expression evaluated to:a[4]=3;

explanation:

prefix ++n                        postfix n++

n=n+1;                           n=n;

n=n;                               n=n+1;

first n++return 3.now n value =3.then ++n means ++3 immediate increaments to 4.

but from your reference on Wikipedia i saw sequence point is matter in that type of expression but i don't get what is sequence point where it ends and start in expression.

0

 

sir i am sending image also

0

consider a simple program

 int i=5;
  i=i++;

what value of i will be assigned ?

+3
0
i will take 5.
0
i am giving you the link of online compiler .just run ones plus use your compiler too.

http://codepad.org/

http://www.tutorialspoint.com/compile_c_online.php

https://ideone.com/

Tell me what value i is printed ?
0

@manoj sir, codepad give 6 

                 tutorial point give 5

                  ideaone give 5.         why?

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
Final value of i is 6
+4 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 .
by Loyal (6.4k points)
+1 vote

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

by Active (2.1k points)
0
So whenever there are 2 or more modifications on the same variable, it is always compiler dependent?
0
No. It is about how and in which order prefix and postfix operator perform its function
0 votes
Answer : option C- assigns 4 to a[4]
by (179 points)
0 votes
what is assigned is compiler dependent......
by (111 points)
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,297 answers
198,265 comments
104,979 users