Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which side of the expression gets evaluated first?

Will the right side of the expression get evaluated first or the left ?

void main ()
{
    int i = 0 , a[3] ;
    a[i] = i++;
    printf ("%d",a[i]) ;
}

2 Answers

The order of evaluation of the operands of the assignment operator is unspecified: the operands may be evaluated in any order.

However, this expression (a[i] = i++) yields undefined behavior because you both modify i (using i++) and you separately read i (using a[i]) without a sequence point in between those actions.

like image 157
James McNellis Avatar answered Nov 29 '25 12:11

James McNellis


C does not define which side gets evaluated first. The standard states (C99 §6.5/2):

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored

The aforementioned result you posted is thereby UB.

like image 34
sysenter Avatar answered Nov 29 '25 11:11

sysenter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!