Logic differences in C and Java
Posted
by paragjain16
on Stack Overflow
See other posts from Stack Overflow
or by paragjain16
Published on 2010-01-08T15:14:02Z
Indexed on
2010/04/10
18:23 UTC
Read the original article
Hit count: 324
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?
© Stack Overflow or respective owner