Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The order of pressing stack of function parameters in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain the order of the stack of C language function parameters. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "C language function parameters stack order" it!

Today we share the question about the stack order of C language function parameters. According to daily habits, the stack order of C language function parameters is from left to right, right? But the fact is the opposite, C language function parameters stack order from right to left. Let's look at a program:

# include

Void stack_test1 (int a, int b, int c)

Int main (void)

{

Int a = 1, b = 2, c = 3

Stack_test1 (a, b, c)

Return 0

}

Void stack_test1 (int a, int b, int c)

{

Printf ("a =% d, & a =% # x\ n", a, (unsigned int) & a)

Printf ("b =% d, & b =% # x\ n", b, (unsigned int) & b)

Printf ("c =% d, & c =% # x\ n", c, (unsigned int) & c)

}

The output of the program is as follows:

A = 1, & a = 0x61fef0b = 2, & b = 0x61fef4c = 3, & c = 0x61fef8

We know that the stack grows downward, that is, allocating memory from the high address to the low address. As can be seen from the program output, the value of variable c is first stored at the high address, followed by b, and finally saved at the low address a. That is, the stack order of the parameters of the function is from right to left.

Why from right to left? The following uses the printf function to analyze:

The prototype of printf function is: int printf (const char * format,...)

As we all know, printf is a variable parameter function. So, how to determine the number of parameters, it depends on format, if format is pushed into the stack first, it is impossible to know how many parameters have not been put into the stack; therefore, format should be the last to enter the stack in order to determine the number of parameters, that is, in line with the parameter stack order of "from right to left" rule.

At this point, I believe that we have a deeper understanding of the "C language function parameter stack order", might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report