Please consider the following statement:
int a[]={1,2,3,4,5,6,7,8};
int i=0,n;
n=a[++i] + i++ + a[i++] + a[i] ;
According to my logic n should be 10. But I am getting different output in c (output is 7) However in java I am getting expected result that is 10. Is there any difference in the way in which increment and decrement operators work in c and java.
Here is my exact c and java code:
#include <stdio.h>
int main()
{
int a[]={1,2,3,4,5,6,7,8};
int i=0,n;
n=a[++i] + i++ + a[i++] + a[i] ;
printf("%d",n);
getch();
return 0;
}
Java code with output : 10
public class HelloWorld{
public static void main(String []args){
int a[]={1,2,3,4,5,6,7,8};
int i=0,n;
i=0;
n=a[++i] + i++ + a[i++] + a[i] ;
System.out.println(n);
}
}