C ++ auto-increment, auto-increment-interview

Source: Internet
Author: User

++ I: Before using I, perform the auto-increment action;

I ++. After I is used, the auto-increment action is performed.

For example: (1) I = 1, j = I ++; // J = I ++, that is, J references the I value first, and then I completes the ++ action.

Printf ("% d, % d \ n", I, j); // The result is I = 2, j = 1

(2) I = 1, j = ++ I; // J = ++ I, that is, I first complete the ++ action, and then J references

Printf ("% d, % d \ n", I, j); // The result is I = 2, j = 2

(3) int I = 1, j = 2;
Int K = I ++ J; // K = (I ++) + J. The value of I ++ is 1, k = 1 + 2 = 3.
Cout <k <Endl; // K = 3

(4) int I = 3, j = 4;
I? I ++: ++ J; // use of conditional operators. If? If it was previously an operational expression, it would be true or false ;? If it is only a number, check whether it is not 0.
Printf ("% d/N", I, j); // I = 3 Non-0, the first expression I ++, ++ J is not executed, so the result is: 4, 4.

(5) Which of the following is the most efficient? x = x + 1, x + = 1, x ++? Why?

X = x + 1 minimum // execution process: read the right X address, x + 1, read the left X address, and pass the right value to the left X (the compiler does not think that the left and right X addresses are the same ).

X + = 1 second // Execution Process: Read the address of the right x, x + 1, and pass the obtained value to X (the address of X has been read) Three Steps

X ++ maximum // Execution Process: Read the X address and perform the X ++ operation in two steps.

(6)

# Include <stdio. h>
# Define product (x) (x * X) // macro defines the macro name as product (x), and the string is (x * X) X as the parameter, that is, the macro operation expression.
Int main ()
{
Int I = 3, J, K;
J = product (I ++); // J = (I ++) * (I ++) Because I ++ is referenced first, j = 9. I ++ twice, so the last I = 5,
K = product (++ I); // K = (++ I) * (++ I), because ++ I is auto-incremented twice and then referenced, so k = 7*7 = 49
Printf ("J = % d, K = % d", j, k); // J = 9, K = 49
Return 0;
}

Reference: http://blog.csdn.net/lutongbin/article/details/6083810

Related Article

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.