+ 1

explain this code

#include <stdio.h> int main() { int arr[]={2,1,9,20}; int a,b,c; a=++arr[1]; b=arr[1]++; c=arr[a++]; printf("%d %d %d",a,b,c); return 0; }

8th May 2020, 1:42 PM
Kawser
Kawser - avatar
5 Antworten
+ 5
Arb Rahim Badsa You are right but accidentally typed arr[3] instead of arr[2] for number 9, oops!! a++ is 2 because post increment, no?
8th May 2020, 2:06 PM
Gen2oo
Gen2oo - avatar
+ 3
Here is the explanation :)) a =++arr[1]; arr[1] is 1 and ++1 is 2. So, a = 2; However, arr[1] is now 2 (not 1 anymore) as well. b = arr[1]++; arr[1] is now 2 and we have used post increment, so it will be as it is (But it will be increased further). Means that, b = 2; Now, c = arr[a++]; a is 2. Because we have used post increment (but will be increased further). Therefore, arr[a++] is arr[2]. Which is 9. At last we have : a = 3; b = 2; c = 9; Therefore the outputs are 3,2 & 9 :))
8th May 2020, 1:57 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 3
Gen2oo Really thanks bro :))
8th May 2020, 2:11 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 2
Arb Rahim Badsa Your explanation is good +1.
8th May 2020, 2:15 PM
Gen2oo
Gen2oo - avatar
0
Gen2oo Thanks everybody. But why b is not print 3?
8th May 2020, 5:31 PM
Kawser
Kawser - avatar