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 C language functions

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about how to use C language functions. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

First, the classification of functions. Library function

Library functions are C language basic libraries that provide a series of functions for programmers to develop software, which can support portability and improve the efficiency of the program.

⚠️ Note: the library function requires a header file.

For example, the header file of the strlen function is

two。 Custom function

Like library functions, custom functions have function names, return value types, and function parameters. But the difference is that we design all these ourselves, which gives programmers a lot of room to play.

⚠️ Note: this function is outside the library function and does not require a header file.

3. Classification of library functions

IO functions: printf, scanf, getchar, putchar

String manipulation functions: strcmp, strlen

Character manipulation function: toupper

Memory operation functions: memcpy, memcmp, memcet

Time / date function: time

Mathematical functions: sqrt, pow

Other library functions

Second, function call 1. Pass value call

The formal parameters and the actual parameters of the function occupy different blocks of memory, and the modification of the formal parameters will not affect the actual parameters.

# includevoid swap (int x, int y) {int z = 0; z = x; x = y; y = z;} int main () {int a = 10; int b = 20; printf ("before exchange =% d% d\ n", a, b); swap (a, b); printf ("after exchange =% d% d\ n", a, b) Return 0;}

two。 Address call

Addressing call is a way of calling a function by passing the memory address of a variable created outside the function to the function parameter.

This operation allows the function to establish a real relationship with the variables outside the function, that is, the inside of the function can directly manipulate the variables outside the function.

Define pointer variables to exchange the values of an and b by accessing the address.

The parameters in the function definition section receive the addresses of variables an and b, and change their values by address.

# includevoid swap (int * pa, int * pb) {int z = 0; z = * pa; * pa = * pb; * pb = z;} int main () {int a = 10; int b = 20; printf ("before exchange =% d% d\ n", a, b); swap (& a, & b) Printf ("after exchange =% d% d\ n", a, b); return 0;}

III. Declaration of the function

Tell the compiler that there is a function, what are the arguments? What is the return type? But it doesn't matter whether it exists or not.

The declaration of a function generally appears before the use of the function, and it should be declared first and then used.

The declaration of the function is usually placed in the header file.

Declare to the function

# includeint main () {int a = 10; int b = 20; / / function declaration-informs int Add (int, int); int c = Add (a, b); printf ("% d\ n", c); return 0;} / function definition int Add (int x, int y) {return x + y;} IV, function definition

The definition of the function refers to the concrete realization of the function and the functional realization of the metasomatic function.

Definition is a stronger statement.

# includeint add (int a, int b) / / function definition {return a + bounding / return the value of a + b} int main () {int a = 0; int b = 0; scanf ("% d% d", & a, & b); int c = add (a, b); / / function call-calculates the value of a + b printf ("% d\ n", c) Return 0;} V, the parameter of the function 1. Formal parameter (formal parameter)

The parameters in the definition part of the function are formal parameters.

two。 Actual parameters (actual parameters)

The parameters in the function call part are arguments.

VI. Practice of functions

1. Using function to find the larger value of two integers

Degree of difficulty: ⭐

# include// find the maximum value-function definition-pass parameter int get_max (int score_one, int score_two) / / function parameter {/ / return a value of score_one if score_one is greater than score_two / / otherwise return the value of score_two / / the value returned is the maximum value return score_one > score_two? Score_one: score_two;} int main () {int score_one = 0; int score_two = 0; scanf ("% d% d", & score_one, & score_two); / / find the maximum function-call-and pass parameter int max= get_max (score_one, score_two); / / function argument printf ("max=%d\ n", max) Return 0;}

two。 Use function to print primes between 100 and 200

Degree of difficulty: ⭐⭐

A prime number is also called a prime number. A prime number refers to a number that is not divisible by other natural numbers except 1 and itself in a natural number greater than 1.

# includeint is_prime (int I) {int j = 0; for (j = 2; j < I; jacks +) / generate except 1 and its own natural number {/ / take I try to divide except 1 and its own natural number if (I% j = = 0) / / divides the entry statement {return 0 After / / returns 0}} / / I and all natural numbers except 1 and itself, the returned value is the prime number return 1 / return 1, that is, the value of the returned prime} int main () {int I = 0; int count = 0 beat / the number of statistical primes for (I = 100; 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