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

How to use function parameters in C language

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use C language function parameters". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use C language function parameters.

First, function parameters

Function parameters are essentially the same as local variables in allocating space on the stack.

The initial value of the function parameter is the actual parameter value when the function is called.

The evaluation order of function parameters depends on the implementation of the compiler.

Let's look at an example of the order in which function parameters are evaluated:

# include int func (int I, int j) {printf ("I =% d, j =% d\ n", I, j); return 0;} int main () {int k = 1; func (kryptonite, kappa +); printf ("% d\ n", k); return 0;}

The output is as follows:

This example shows that the order in which function parameters are evaluated depends on the implementation of the compiler.

Second, the sequence point of the program

There are certain sequence points in the program.

A sequential point refers to the latest time when the value of a variable is modified during execution.

When the program reaches the sequence point, the previous-cut operation must be completed

At the end of each complete expression, at the semicolon

&, | |,?:, and after each parameter of the comma expression is calculated

After all arguments are evaluated when the function is called (before entering the function body)

Let's look at an example of a sequence point in a program:

# include int main () {int k = 2; int a = 1; k = knot + + kryptonite; printf ("k =% d\ n", k); if (a muri-& & a) {printf ("a =% d\ n", a);} return 0;}

The output is as follows:

A & & a, for the & & operator, each Operand is an order point. When the program executes from left to back, a muri-the memory modification must be done immediately, so a becomes 0.

Why output 6? Let's run the code in VS2012 to disassemble:

This assembly code is simply a + operation, k = 2 + 2 = 4, and then two + operations, so the final result is 6.

III. Summary.-up.

The parameters of the function allocate space on the stack

The arguments of the function do not have a fixed order of calculation.

The sequence point is the latest time for variable modification in C language.

IV. Calling convention

The calculation order of function parameters depends on the compiler, so how to determine the stack order of function parameters?

When a function call occurs

Parameters are passed to the called function

The return value is returned to the function caller.

The calling convention describes how parameters are passed to the stack and how the stack is maintained.

Parameter transfer sequence

Call stack cleanup

The calling convention is predefined, which can be understood as calling protocol.

Calling conventions are usually used when library calls and library development

Enter the stack from right to left: _ stdcall,_cdecl,_thiscall

Enter the stack from left to right: _ pascal,_fastcall

5. Variable parameters

When calculating the average, we can generally write it as follows:

# include float average (int array [], int size) {int iTuno; float avr = 0; for (iTun0; I)

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

Development

Wechat

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

12
Report