Redirected
edited by
14,504 views
17 17 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

8 Answers

Best answer
17 17 votes

It is compiler dependent

Reference :Undefined behaviour

selected by
9 9 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 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 2 votes
Correct Answer - Option 4 : what is assigned is compiler dependent

 EXPLANATION:
This question can be solved in two different ways by different compilers:
 1. First perform n++ then calculate ++n for index value
2. Calculate ++n for index value initially and then find n++. Therefore different compilers can solve this code in one of the above ways. Hence, the correct answer is "option 4".
Increment operation is of two types:
1. Pre increment (++n): Increase the value first & then substitute the value in variable n.
2. Post increment (n++): Initially substitute the value in n & then increase the value of n.
Answer:
Position:
Show:

Related questions

1 1 vote
0 0 answers
842
842 views
PEKKA asked Nov 18, 2016
842 views
What are sequence Points ....?A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations...
0 0 votes
1 1 answer
777
777 views
Ankush Tiwari asked Aug 20, 2016
777 views
#include<stdio.h int main() { char *p1="xyz"; char *p2="xyz"; if(p1==p2) printf("equal"); else printf("unequal"); }Output is equal how??? please explain
1 1 vote
1 1 answer
1.6k
1.6k views
Prajwal Bhat asked Aug 19, 2016
1,623 views
#include<stdio.h>int main(){int a = 10, b = 20, c = 30, d = 40;printf("%d%d%d",a, b, c);printf("%d%d%d", d);return 0;}What is the Output and when I run it I am getting so...
2 2 votes
1 answers 1 answer
819
819 views
Registered user 7 asked Aug 19, 2016
819 views
#include<stdio.h #include<stdlib.h int main() { int p=9; printf("%d %d",p++,++p); } how it executed and also how printf function executed left to right or right to left?