Analysis of auto-incrementing operators in C Language

Source: Internet
Author: User

The discussion about auto-increment operations such as a ++ and ++ A in C language is boring, but sometimes I have to discuss it in detail to cope with interviews.

Environment: win7, vs2010


Example:

Write and judge whether the four expressions of ABCD are correct. If yes, write the value of A in the expression (3 points)

Int A = 4;
(A) A ++ = (a ++); (B) a ++ = (++ A); (c) (a ++) ++ = A; (d) (++ A) ++ = (a ++ );

Q: A =?


A: C error. The left side is not a valid variable and cannot be assigned a value. You can change it to (++ A) + =;

After the change, the answers are 9, 10, 10, and 11 in sequence.


Analysis:

A option A + = (a ++ );

Int _ tmain (INT argc, _ tchar * argv []) {int A = 4; 00f7136e mov dword ptr [a], 4 A ++ = (a ++ ); 00f71375 mov eax, dword ptr [a] // Save the value of a to eax. In this case, eax = 400f71378 add eax, dword ptr [a] // Add a to the value of eax, in this case, eax = 800f7137b mov dword ptr [a] and eax // save eax to A, that is, a = 8. It can be seen that a + = (a ++) first, run a = a + A; 00f7137e mov ECx, dword ptr [a] // save a to ecx00f71381 add ECx, 1 // ECx + 1, then ECx = 9; 00f71384 mov dword ptr [a], ECx // ECx is stored back to A, that is, a = 9 return 0; 00f71387 XOR eax, eax}

B Option A + = (++ );

Int _ tmain (INT argc, _ tchar * argv []) {int A = 4; 0129136e mov dword ptr [a], 4 A ++ = (++ ); 01291375 mov eax, dword ptr [a] 01291378 add eax, 1 // Add 1 first, then eax = 50129137b mov dword ptr [a], eax // eax is stored back to, in this case, a = 50129137e mov ECx, dword ptr [a] 01291381 add ECx, dword ptr [a] // ECx = ECx + ECx. In this case, ECx = 1001291384 mov dword ptr [a], ECX // at this time a = 10 return 0; 01291387 XOR eax, eax}

C option: (A ++) + =;

Compilation error: Error c2106: '+ =': left operand must be L-value. (A ++) is an expression and cannot be assigned a value.


D option: (++ A) + = (a ++ );

Int _ tmain (INT argc, _ tchar * argv []) {int A = 4; 0129136e mov dword ptr [a], 4 (++) + = (a ++); 00d81375 mov eax, dword ptr [a] 00d81378 add eax, 1 00d8137b mov dword ptr [a], eax // complete ++, at this time, a = 5; 00d8137e mov ECx, dword ptr [a] 00d81381 add ECx, dword ptr [a] // complete a = a + A; then a = 10; 00d81384 mov dword ptr [a], ECx 00d81387 mov edX, dword ptr [a] 00d8138a add edX, 1 00d8138d mov dword ptr [a], EDX // complete a ++; at this time, a = 11 return 0; 01291387 XOR eax, eax}


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.