1. The parameters of the prinf function contain pointer expressions. In what order are they calculated? The result of the code operation is obviously not from left to right.
#include <stdio.h>
int main() {
int a[5] = { 1,2,3,4,5 };
int *p = a;
printf("%d\n", *p);
printf("%d %d %d %d\n", *(++p)++,*p, *p++, *p);
getchar();
return 0;
}
Changing a variable multiple times in one statement is undefined behavior and may have different results on different platforms. This question makes no sense.
printf{"%d",++i}
represents two operationsFirst execute i=i+1, then output i
And i++ means
First output, then execute i=i+1
The order of operation of function parameters has little to do with the internal logic of the function. It should calculate
++p
before pushing it onto the stack (before the function is executed), and then calculatep++
after the function ends. If you want to know the specific sequence, you can refer to the assembly code (the specific meaning will be updated tomorrow, sorry)Digression:
rrreee2 and 3 that appear in the results can still be explained.
4 is very strange. If I have to explain it reluctantly, the
++
outside the brackets of*(++p)++
also works forp
,but the operator is in the form of
p++
, which should be in It is incremented after the statement ends, so this explanation is obviously wrong.I am in
Cygwin + gcc (GCC) 5.4.0
environment. The running results are as follows. 4 does not appear. What environment did you use?The order in which function parameters are pushed onto the stack is certain, but the order in which the
parameters are evaluated is not specified
. Thecompiler only guarantees that the values of all parameters are known before
printf
is calledInformation on this is available Search
Sequence Point