is the output of printf ("%d,%d\n", i++, ++i) ok???

Source: Internet
Author: User

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???

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.