Why is the C-language function parameter stacking order from right to left? (from left to right, the one who touches printf will get into a dead loop)

Source: Internet
Author: User

Last semester to learn the assembly language, and in the operating system experiment using the Assembly + C language mixed programming, the middle also understand some C language and the corresponding relationship between Assembly languages.
Because assembly language is the underlying programming languages, all function parameters have to be directly controlled stack access, in the mixed programming, to use the assembly to call the C function, of course, you need to know the parameters of the pressure stack situation.
When we know that the parameters of the C function are stacked from right to left, I find it strange, because most of the time, people's habits are from left to right, it is difficult for designers to learn our Chinese ancient writing from right to left habits do not?
At that time just wrote down the rules, and did not go into the reasons for this, and later in the experiment with the Assembly to achieve the basic functions of printf and scanf function, tasted the pressure stack from right to left benefits, but still do not think much.
It was not until the first time that I saw Stanford Open class that the teacher explained it.
So, what is this for?
To answer this question, you have to talk about the printf () function, which is the prototype of the printf function: printf (const char* Format,... )
Yes, it is an indefinite parameter function, so how do we know the number of parameters in actual use? This depends on format, where the compiler determines the number of parameters by the number of% placeholders in format.
Now we assume that the parameters of the stack order is from left to right, when the function calls, format the most advanced stack, followed by the various parameters into the stack, the last PC into the stack, at this time, because of the format advanced stack, the above pressure on the unknown number of parameters, to know the number of parameters, you must find format, To find format, you have to know the number of parameters, so you are trapped in an impossible to solve the dead loop!!
And if the parameters from right to left pressure stack, what is the situation? Function call, first put a number of parameters are pressed into the stack, and then press format, and finally press the PC, so that the top pointer plus 2 to find the format, through the% placeholder in format, get the number of the following parameters, so that all parameters are correctly obtained.
So, if it doesn't exist ... With this variable parameter, the stacking order of the parameters is not related either from left to right or from right to left.

http://m.blog.csdn.net/article/details?id=47381597

Why is the C-language function parameter stacking order from right to left? (from left to right, the one who touches printf will get into a dead loop)

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.