Does there exist any order for evaluation of expressions in case of an array. If an expression E is of the form E1[E2], where E1 & E2 are also expressions, is the order of evaluation of E1 & E2 fixed ?
Here is my code :
#include<stdio.h>
int main(){
int a[5] = {1,2,3,4,5};
(a + printf("1"))[printf("2")];
(printf("3"))[a + printf("4")];
return 0;
}
It's showing output as: 1243
I'v compiled it with gcc.
Thanks.
Only the sequential-evaluation ( , ), logical-AND ( && ), logical-OR ( || ), conditional-expression ( ? : ), and function-call operators constitute sequence points, and therefore guarantee a particular order of evaluation for their operands.
Yes, of course it does matter - js is evaluated from left to right, and the || operator does short-circuit.
The subscript operator is defined as square brackets [] . It is used to access the elements of string, list tuple, and so on. Let's discuss the syntax with an example: <given string>[<index>] The given string is the string you want to examine and the index is the position of the character you want to obtain.
The subscript operator is commutative. Therefore, the expressions array[index] and index[array] are guaranteed to be equivalent as long as the subscript operator is not overloaded (see Overloaded Operators). The first form is the most common coding practice, but either works.
E1[E2]
is equivalent of *(E1 + E2)
.
And the Standard tells that "the order of evaluation of subexpressions and the order in which side effects take place are both unspecified". So, the order of evaluation of E1[E2]
is not fixed.
N1256:
6.5 Expressions
...
3 The grouping of operators and operands is indicated by the syntax.74) Except as specified later (for the function-call()
,&&
,||
,?:
, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
So the short answer is "no"; for an expression like E1[E2]
, the order in which the subexpressions E1
and E2
are evaluated is not fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With