In-depth discussions on printf

Source: Internet
Author: User

Int A = 15;

Printf ("% d \ n", ++ A, A ++ );

There is nothing to say about the old problem, but the parameters involved in the sequence points have not been carefully considered. Therefore, the internal principle is analyzed.

My original consideration was to press a ++ into the stack in the order of cdecl pressure, which was 15. Then, ++ experienced side effects, a changed to 16, and then pushed to ++, A first Auto-increment, 17, so the output result is 17 15. Although I felt uneasy, I had to deceive myself for a while. I verified it with GCC, and then vc6 gave me a stick, with the output being 16 or 15.

To tell the truth, I have not studied the order of the side effects in the parameters, so this result is caused, so I have a good look at the assembly code they produced:

Vc6 compilation code:

5: int A = 15;
00401028 mov dword ptr [ebp-4], 0fh
6:
7: printf ("% d \ n", ++ A, A ++ );
0040102f mov eax, dword ptr [ebp-4]
00401032 mov dword ptr [ebp-8], eax
00401035 mov ECx, dword ptr [ebp-8]
00401038 push ECx
00401039 mov edX, dword ptr [ebp-4]
0040103c add edX, 1
0040103f mov dword ptr [ebp-4], EDX
00401042 mov eax, dword ptr [ebp-4]
00401045 push eax
00401046 push offset string "% d \ n" (00422020)
0040104b mov ECx, dword ptr [ebp-4]
0040104e add ECx, 1
00401051 mov dword ptr [ebp-4], ECx
00401054 call printf (004010a0)
00401059 add ESP, 0ch

 

It can be seen that at 0x00401038, the third parameter is 15, 0x0040103c, and the side effect of a ++ occurs, and a changes to 16; 0x00401045 is pushed to the second parameter as 16, which does not occur until the side effects of ++ A at 0x0040104e, and the output result is 16 15. I feel that the comma Separator of the function parameters here is not a comma expression, so it is not an order point, resulting in the side effects of ++ a not necessarily occurring here, so VC does not add the value of a at this time. Is that true? For function parameters, the Order points are [to be studied] at the end of the function.

Let's take a look at the situation of GCC 4.3. The following is the assembly code of the same program:

0x00401146 <main + 22>: movl $0xf,-0x8 (% EBP)
0x0040114d <main + 29>: mov-0x8 (% EBP), % eax
0x00401150 <main + 32>: addl $0x1,-0x8 (% EBP)
0x00401154 <main + 36>: addl $0x1,-0x8 (% EBP)
0x00401158 <main + 40>: mov % eax, 0x8 (% ESP)
0x0040115c <main + 44>: mov-0x8 (% EBP), % eax
0x0040115f <main + 47>: mov % eax, 0x4 (% ESP)
0x00401163 <main + 51>: movl $0x4020a0, (% ESP)
0x00401_a <main + 58>: Call 0x4011d8 <printf>

It can be seen that at 0x00401150 and 0x00401154, A is automatically increased twice, and the third parameter of 0x00401158 is 15, 0x0040115f is pushed to the second parameter, at this time, after 0x0040115c, the second parameter is 17, so the result is 17 15.

Although it is a problem of supporting the end, there is no need to tangle, but it still leads to a lot of uncertain things, it is a learning.

 

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.