+ 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; }
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?
+ 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 :))
+ 3
Gen2oo Really thanks bro :))
+ 2
Arb Rahim Badsa
Your explanation is good +1.
0
Gen2oo
Thanks everybody.
But why b is not print 3?