Relationship between the implementation of the variable length parameter table printf in C language and the order of the parameters in the stack

Source: Internet
Author: User

"The feature of C language from right to left into Stack enables it to implement variable-length parameter functions such as printf"

This is a true proposition. I checked some materials at night and summarized them as follows:

(1) differences between the variable length parameter list and fixed parameters:

Most of the functions we write are fixed parameter functions (at least I have not written a variable-length function). After compilation, the address of the form parameter is fixed, and the called function also knows the address of the form parameter, so the value can be correct. For example, int func (int A, int B)

(2) How does the printf () Variable Length Parameter take the value?

In the prototype of the printf function, the first parameter is unchanged and must be a format string. the number and content of the subsequent parameters depend on the content of the first parameter, which is also called the variable length parameter. In the implementation of printf, it is not difficult to see that the called function can determine the parameter type and number only after obtaining the first parameter, therefore, the compiler must place the first parameter in a fixed position. This location is % EBP + 8.

 

 

After the func function is called, % EBP, % ESP registers point to memory units, % EBP + 8 points to the first parameter & arg1. (the format string in the printf function). If the stack is pressed from left to right, the first parameter is not at the top of the stack, but at the bottom of the stack, you cannot know its specific location.

 

In short, the C language specifies that the function pressure stack sequence is from right to left, so that the first parameter is always at the top of the stack that calls the function, and the called function can always get it correctly, to continue to take other parameters.

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.