The principle of C-language function parameters transfer

Source: Internet
Author: User

There are two ways to pass the parameters in C language: one is to pass the form of a stack. There is also a way to pass through registers.

This. We're just describing the first way to pass the number of passes, and the second way is not specifically described here.

First, let's take a look at one of the following simple invocation routines:

int Add (int a, int b, int c)

{

return a+b+c;

}


void Main ()

{

int x =0, y = 1, z = 2;

int result = 0;

result = Add (x, y, z);

printf ("Result for x+y+z%d", result);

}

With these routines, we'll look at how the parameters are passed.

First of all. We understand a knowledge point, that is, the "()" operator in C language has a lot of meaning, for example, binding characters, function calls and so on.

The result = Add (x, y, z) statement is, of course, a function call, so in the compile phase, the run-time after the function call is encountered, starting with the last parameter. The stack order is z->y->x until all of the references are pressed into the call stack and then called to add the corresponding function address to start running. After the press stack has finished running. Stack space, from top to bottom in order of x->y->z (because the stack is growing in the direction of upward, that is, the address is low, so x in the lowest address, that is, the top of the stack). So. The reading and manipulation of the parameters in the called function are also occurring in the stack.

The principle of C-language function parameters transfer

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.