Introduction to the order of function parameters in C Language

Source: Internet
Author: User

Stack, I think everyone knows. STACK: hardware. A Data Structure is a special linear table that can only be inserted and deleted at one end. The following describes the order of function parameters in C.
People who are persistent in technology, such as me, often want to "know its nature" and "know its nature" for some problems ". C language is profound and profound. Even if I have many years of development experience, I still have many questions. Someone asked me one day what is the order of function parameters in C language in the stack? From right to left, I will answer the question at will. Why is it from right to left? After all, I did not give a reasonable explanation. So I had to do a job, so I had this blog post.
 
Running result:
X = 100 at [BFE28760]
Y = 200 at [BFE28764]
Z = 300 at [BFE28768]
The C program stack bottom is a high address, and the stack top is a low address. Therefore, the above instance can indicate that the function parameter's import sequence to the stack is indeed from right to left. But why? According to the literature, the parameter stack order is related to the specific compiler implementation. For example, in Pascal, parameters are written from left to right into the stack, and some languages can also be specified through modifiers, such as Visual C ++. either method works. Why does the C language need to be right-to-left?
It is further found that Pascal does not support variable length parameters, but C supports this feature, which makes the order of C function parameters in the stack from right to left. The specific cause is that the number of parameters can be dynamically changed because of the advantage of the C-mode parameter entry sequence (from right to left. Stack heap analysis shows that the top parameters of the stack are pressed at the bottom of the stack from left to right. Unless you know the number of parameters, you cannot obtain the leftmost parameter through the relative displacement of the stack pointer. This makes the number of parameters on the left uncertain, which is the opposite of the number of dynamic parameters.
Therefore, C language function parameters adopt the order of inbound Stack from right to left, mainly because they support variable length parameters. In other words, if this feature is not supported, the C language is exactly the same as Pascal and uses the parameter stack mode from left to right.

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.