Logic differences in C and Java
- by paragjain16
Compile and run this code in C
#include <stdio.h>
int main()
{
int a[] = {10, 20, 30, 40, 50};
int index = 2;
int i;
a[index++] = index = index + 2;
for(i = 0; i <= 4; i++)
printf("%d\n", a[i]);
}
Output : 10 20 4 40 50
Now for the same logic in Java
class Check
{
public static void main(String[] ar)
{
int a[] = {10, 20, 30, 40, 50};
int index = 2;
a[index++] = index = index + 2;
for(int i = 0; i <= 4; i++)
System.out.println(a[i]);
}
}
Output : 10 20 5 40 50
Why is there output difference in both languages, output is understandable for Java but I cannot understand output in C
One more thing, if we apply the prefix ++ operator, we get the same result in both languages, why?