"Learn C language in one minute every day·11" static static, inline function, macro function, sorting

1. The difference
between static static modifier
static global variables and ordinary global variables.
Static global variables can only be used in this file. Ordinary global variables can be used in all files under a project, and extern is required. static global variables can only be initialized once

The difference between static local variables and ordinary local variables:
static local variables can only be initialized once
. For example:

while(i-- <3)
{
    
    
	static int j = 0
	j++; //第一次循环j==1,第二次j==2,第三次j==3
}

The difference between static functions and ordinary functions:
Static functions can only be used in this file
. Ordinary functions can be called in other files, but a header file containing the declaration of this function needs to be added to the file. Or declare the function of this other file directly in the file global, and then call this function in this file

2.
Inline function
The definition of the inline function and the code that calls the function must be in the same file. If there are multiple files that need to use an inline function, the inline function is generally defined in the header file. Header files generally only contain function declarations, but inline functions are an exception and need to be defined in the header file.

# include<stdio.h>
inline static void eatline()
{
    
    
	printf(“haha”);
}
int main(void)
{
    
    
	eatline(); //调用内联函数
}

Functional macros generally do not have return values, while inline functions can have return values.

3.
Quick sort function qsort()

void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))

The first element is the address of the first element of the array.
The second element is the length of the array.
The third element is the byte size of each element.
The fourth element is passed in the function address of a comparison function.
For example, a double array is sorted from small to large.

int compar(const void *p1, const void *p2)
{
    
    
	const double * a1 = (const double *)p1; //必须强转
	const double *a2 = (const double *)p2;
	if(*a1 < *a2)
		return -1;
	else if (*a1 == *a2)
		return 0;
	else
		return 1;
}

4.
Linear structures: arrays (continuous storage), linked lists (discrete storage)
Common applications of linear structures: stacks and queues
Non-linear structures: trees and graphs

Search: half search
Sort: bubble, insert, select, quick sort, merge

5.
Macro function

#include <stdio.h>
#define SUM(n) do {
      
       \
	int sum = 0; \
	while (n > 0) {
      
       \
		sum = sum + n; \
		n--; \
	} \
	printf("前n项和为:%d\n", sum); \
} while (0)

int main(void)
{
    
    
	int n = 5;
	SUM(n);  //15
	return 0;
}

The execution step of the macro function is to replace the variables or constants first, and then put them in the main function. Here n is the variable. The first step is to replace:

do {
    
     
	int sum = 0; 
	while (n > 0) {
    
     
		sum = sum + n; 
		n--; 
	} 
	printf("前n项和为:%d\n", sum); 
} while (0)

The second step is placed in the main function:

int main(void)
{
    
    
	int n = 5;
	do {
    
     
			int sum = 0; 
			while (n > 0) {
    
     
				sum = sum + n; 
				n--;  //n是个变量可以自减操作,刚开始为5
			} 
			printf("前n项和为:%d\n", sum); 
	} while (0);
	return 0;
}

If you directly assign constants to the macro function in the main function, such as SUM(5);, an error will be reported. The first step is to replace:

do {
    
     
	int sum = 0; 
	while (5 > 0) {
    
     
		sum = sum + 5; 
		5--;   //5 = 5-1; error常量是不能修改的左值,可在刚开始定义一个变量,如int i = n; 后面用i计数和自减就不会报错
	} 
	printf("前n项和为:%d\n", sum); 
} while (0)
Insert image description here

Guess you like

Origin blog.csdn.net/cs1395293598/article/details/135464950