preface:
● due to the limited level of the author, it is inevitable that there are fallacies in the article. Please correct the slang, and sincerely hope to give advice!
Function explanation (1)
goto statement:
#include <stdio.h> //Code 1 int main() { next: printf("hehe\n"); printf("haha\n"); //goto next; // This code will loop indefinitely return 0; }
C language provides goto statements that can be abused at will and labels that mark jump. In theory, goto statement is not necessary. In practice, it is easy to write code without goto statement. However, goto statements are still useful in some situations. The most common usage is to terminate the processing process of the program's deeply nested structure. For example: jump out of two or more layers of loops at a time. In the case of multi-layer loops, it is not possible to use break. It can only exit from the innermost loop to the upper loop.
goto language is really suitable for the following scenarios:
for(...) for(...) { for(...) { if(disaster) goto error; } } ... error: if(disaster) // Handling error conditions
The following is an example of using the goto statement, and then replacing the goto statement with a circular implementation:
Here's how to use goto Statement, and then replace it with a circular implementation goto sentence: #include <stdio.h> int main() { char input[10] = {0}; system("shutdown -s -t 60"); again: printf("The computer will shut down within 1 minute. If you enter: I am a pig, cancel the shutdown!\n Please enter:>"); scanf("%s", input); if(0 == strcmp(input, "I'm a pig")) { system("shutdown -a"); } else { goto again; } return 0; } And if not goto Statement, you can use a loop: #include <stdio.h> #include <stdlib.h> int main() { char input[10] = {0}; system("shutdown -s -t 60"); while(1) { printf("The computer will shut down within 1 minute. If you enter: I am a pig, cancel the shutdown!\n Please enter:>"); scanf("%s", input); if(0 == strcmp(input, "I'm a pig")) { system("shutdown -a"); break; } } return 0; }
Extension of shutdown command - (please click here)
Send. exe version or Release version
int main() { char arr[] = "abc"; //size_t -> unsigned int size_t len = strlen(arr); printf("%u\n", len); //%d - signed //%u - unsigned return 0; }
Custom function
If library functions can do everything, what do programmers do?
All the more important is custom functions. Like library functions, custom functions have function names, return value types, and function parameters. But the difference is that these are designed by ourselves. This gives programmers a lot of room to play.
Function composition:
ret_type fun_name(para1, * ) { statement;//Statement item } ret_type Return type fun_name Function name para1 Function parameters
Write a function to find the maximum value of two integers:
int get_max(int x, int y) { int z = 0; z = (x > y ? x : y); return z; } int main() { int a = 0; int b = 0; scanf("%d %d", &a, &b); int m = get_max(a, b); //int m = get_max(10+22, get_max(5, 40));// Variable, constant, expression, function printf("%d\n", m); return 0; }
void menu() { printf("****** 1. play *****\n"); printf("****** 0. exit *****\n"); } int main() { menu(); //menu() Write two statements menu()Call twice return 0; }
void Swap1(int x, int y) { int z = 0; z = x; x = y; y = z; } int main() { int a = 0; int b = 0; scanf("%d%d", &a, &b); //int m = get_max(a, b); //Swap 2 variables printf("Before exchange:a=%d b=%d\n", a, b); // Swap1(a, b);// Value passing call // Swap2(&a, &b);// Address call printf("After exchange:a=%d b=%d\n", a, b); return 0; }
a. B no connection is established. When the function is called, the argument is passed to the formal parameter, which is actually a temporary copy of the argument
Therefore, the modification of formal parameters will not affect the arguments, so there is no problem with the syntax, but there is a problem with the logic, and the exchange task cannot be completed!
Here we can see as like as two peas, Swap1 and x have their own space, and have the same content as the real reference, when the function of the y is called. So we can simply think that after the formal parameter is instantiated, it is actually equivalent to a temporary copy of the argument.
Arguments and formal parameters do not use the same space!
Optimize the use of pointers:
void Swap2(int* pa, int* pb) { int z = 0; z = *pa; *pa = *pb; *pb = z; } int main() { int a = 0; int b = 0; scanf("%d%d", &a, &b); //Swap 2 variables printf("Before exchange:a=%d b=%d\n", a, b); // Swap1(a, b);// Value passing call // Swap2(&a, &b);// Address call printf("After exchange:a=%d b=%d\n", a, b); return 0; }
Actual parameter (actual parameter):
The parameters really passed to the function are called arguments. Arguments can be constants, variables, expressions, functions, etc. No matter what type of arguments are, they must have definite values when making a function call in order to pass these values to the formal parameters.
Formal parameter (formal parameter):
Formal parameters refer to the variables in parentheses after the function name. They are called formal parameters because they are instantiated (memory units are allocated) only when the function is called. Formal parameters are automatically destroyed when the function call is completed. Therefore, formal parameters are only valid in functions.
Formal parameters are not dynamically allocated!
Value passing call
The formal parameters and arguments of the function occupy different memory blocks respectively, and the modification of the formal parameters will not affect the arguments.
Address call
Addressing call is a way to call a function by passing the memory address of the variable created outside the function to the function parameters.
This parameter transfer method can establish a real relationship between the function and the variables outside the function, that is, the variables outside the function can be directly operated inside the function.
Write a function to judge whether a number is a prime number. is_prime() A prime number returns 1, not a prime number returns 0 int is_prime(int n) { //Digital trial division of 2~n-1 int j = 0; for (j = 2; j < n; j++) { if (n % j == 0) { return 0; } } return 1;//prime number } #include <math.h> int is_prime(int n) { //Digital trial division of 2~n-1 int j = 0; for (j = 2; j <= sqrt(n); j++) { if (n % j == 0) { return 0; } } return 1;//prime number } int main() { int i = 0; for (i = 100; i <= 200; i++) { //Judge whether i is prime - if it is prime, print i if (is_prime(i)==1) { printf("%d ", i); } } return 0; }
Write a function to judge whether a year is a leap year Returns 1 for leap years, not 0 for leap years int is_leap_year(int y) { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) { return 1; } else { return 0; } } //int is_leap_year(int y) //{ // return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)); //}Not all controls have return values. This writing is wrong! It's right! The function function should be single, so it has good portability! int main() { int y = 0; int count = 0; for (y = 1000; y <= 2000; y++) { //Judge whether y is a leap year if (is_leap_year(y) == 1) { count++; printf("%d ", y); } } printf("\ncount = %d\n", count); return 0; }
Binary search:
Write a function to realize the binary search of an integer ordered array. Return subscript when found Return not found-1 int binary_search(int *arr, int k) int binary_search(int arr[], int k, int sz) { // 4 /4 int left = 0; int right = sz - 1; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] < k) { left = mid + 1; } else if (arr[mid] > k) { right = mid - 1; } else { return mid; } } return -1;//I just can't find it } int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int k = 17; //Pass array arr to binary_ The search function actually passes the address of the first element of the ARR array int sz = sizeof(arr) / sizeof(arr[0]); int ret = binary_search(arr, k, sz); if (-1 == ret) { printf("can't find\n"); } else { printf("Yes, the subscript is%d\n", ret); } return 0; }