An application example of indefinite parameter in C language

Source: Internet
Author: User
Tags printf

An application example of indefinite parameter in C language: indefinite parameters as a special skill of the middle-C + + language was praised by many people, but in fact this technology has not been applied much. I don't see much application except formatted output. The main reason is that this technology is more cumbersome, side effects are more, and in general the overloaded function is enough to replace it. However, since you are interested in it, let me briefly summarize its usage and common problems that need attention.

Just learn C language, the General people will first contact printf function. With this function, you can print an indefinite number of variables to the screen, such as:

printf ("%d", 3);

printf ("%d,%d", 3,4);

The above code seems simple, but in fact we need to solve many problems. When we were designing printf, we didn't know how many parameters were going to be passed in. In this unknown situation, we need to address the following issues:

How do we tell printf we're going to pass in some parameters

How does printf access these parameters

How the system frees the parameters from the delivery stack when the function call is complete

To solve these problems, we first want to explain the cdecl calling convention, where all functions using indefinite parameters must be calling conventions using CDECL (global function) or this call (class member function). This convention provides for parameter passing as follows:

Parameters from right to left into the stack (that is, if you call F (a,b,c), C first into the stack, then B, and finally a into the stack)

The caller is responsible for cleaning up the stack

The 2nd directly solves the third problem in the previous three questions. Let's say two other questions in detail.

Determine the number of parameters

In a function, you typically have the following Prelog code:

00401020 Push EBP

00401021 mov Ebp,esp

00401023 Sub esp,48h

After the above code is executed, the stack context in which the Func (a,b,c) function functions becomes the following layout:

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.