I have been trying to understand of this following C-program:
#include <stdio.h>
int arr[] = {1,2,3,4};
int count;
int incr(){
return ++count;
}
int main(){
arr[count++]=incr();
printf("%d %d",count,arr[count]);
return 0;
}
The program gives 1 2 as output,what I am not gtting is why the value of count is 1 here and not 2 (since there are two increments)?
The order of evaluation of operands of
=operator is unspecified inarr[count++]=incr();and since both the operands are trying to modify the same global variablecountthe result would be different on different compilers depending upon the order of evaluation.EDIT
Actually the behavior is undefined (which means anything can happen) because “the prior value of the variable
countis not accessed (only) to determine the value to be stored.”