1. Description of the problem
What is the output of the following code?
Topic 1:
int i=;p rintf ("%d,%d\n", i++, ++i);
Topic 2:
int 3 ;p rintf ("%d,%d,%d,%d,%d\n", i++, ++i, I, i++, i);
2. Problem solving ideas "wrong"
The printf parameters are stacked from right to left, so:
- The output of topic 1 is: 11,12
- The output of Topic 2 is:
3. Reflection
- Note: This kind of topic compiler is different, the result will be different, that is, this behavior depends on the compiler!!! No need to tangle.
- Cause Analysis:
- The C/C + + language does not specify a specific stacking order, there is no normalization when it supports a function without fixed parameters, so in order to achieve this time most compilers use a right-to-left compression stack, but the standardization of the requirements of at least one fixed parameter, this limit is not necessary. right-to-left, however, has almost become the order in which the C compiler is accustomed. C + + 's _stdcall Way is also used from right to left, different just do not need the caller to manually clear the stack.
- In addition, the Order of evaluation and the stacking order are two different things. , there is hardly any provision in the C language for the Order of evaluation, and the compiler can find out the value before deciding how to press the stack.
- Simply put, this problem is related to the compiler's implementation of language rules. The code generated under different compilers may be different, and the results will be different. Don't tangle with this code that can cause errors.
What ' s the value of i++ + i++?
It ' s undefined. Basically, in C and C + +, If you read a variable twice in an expression where you also write it, the result is unde Fined. Don ' t do that. Another example is:
V[i] = i++;
Related example:
f (v[i],i++);
Here, the result was undefined because the order of evaluation of function arguments is undefined.
Having the order of evaluation undefined are claimed to yield better performing code. Compilers could warn about such examples, which is typically subtle bugs (or potential subtle bugs). I ' m disappointed that after decades, the most compilers still don ' t warn, leaving-job to specialized, separate, and under Used tools.
is the output of
printf ("%d,%d\n", i++, ++i) ok???