# C language review

I've been learning data structure recently. Let's review the c language first

### (1) Data

#### 1. Constants and variables #### 2. Data type ### (2) Operator Operator prioritization:

! Non > arithmetic operator > relational operator > & & and, | or > assignment operator

### (3) Statement

#### 1. Assignment statement

```#include<stdio.h>
int main()
{
int a, b;
a = 1; //Assign 1 to a
b = 2; //Assign 2 to b
printf("%d %d %d\n", a, b, a+b);
a += 2; //Equivalent to a = a+2, assign a+2 to a
b *= 2; //Equivalent to B = b*2, assign b*2 to B
printf("%d %d %d\n", a, b, a+b);
char c1, c2;
c1 = 'a'; //Assign 'a' to c1
c2 = c1 + 1; //Assign c1 + 1 to C2, and c1 + 1 represents the ASCII code + 1 of c1
printf("%c %d\n", c1, c1);
printf("%c %d\n", c2, c2);
return 0;
}
```

#### 2. Input / output statements

##### (1) Input / output functions scanf() and printf()
```#include<stdio.h>
int main()
{
int a;
scanf("%d", &a); //Enter decimal basic integer
printf("%d %o %x\n", a, a, a); //Output decimal, octal and hexadecimal basic integers
short b;
scanf("%hd", &b); //Enter decimal short
printf("%hd %ho %hx\n", b, b, b); //Output decimal, octal and hexadecimal short integers
long c;
scanf("%ld", &c); //Entered decimal long
printf("%ld %lo %lx\n", c, c, c); //Output decimal, octal and hexadecimal long integers
float d;
scanf("%f", &d); //Input single precision floating point
printf("%f\n%6.2f\n%-6.2f\n", d, d, d); //Output in the default format, with two decimal places reserved for right alignment of six digits and two decimal places reserved for left alignment of six digits
double e;
scanf("%lf", &e); //Input double precision floating point
printf("%lf\n%6.2lf\n%-6.2lf\n", e, e, e); //Output in the default format, with two decimal places reserved for right alignment of six digits and two decimal places reserved for left alignment of six digits
return 0;
}
```
```#include<stdio.h>
int main()
{
char c1;
scanf("%c", &c1); //Enter character type by entering characters
printf("%c\n", c1); //Output character type
char c2;
scanf("%d", &c2); //Enter the character type by entering ASCII code
printf("%c\n", c2); //Output character type
char c3;
scanf("%s", &c3); //Enter a string of the specified length
printf("%s", c3); //Output string
return 0;
}
```
##### (2) Character input / output functions getchar() and putchar()
```#include<stdio.h>
int main()
{
char c1, c2;
c1 = getchar(); //If you press enter after entering the character of c1, enter, that is' \ r 'will be assigned to c2
c2 = getchar();
putchar(c1);
putchar(c2);
putchar('\n');
return 0;
}
```

#### 3.if statement

```//The grades given to students are 90 ~ 100 as A, 80 ~ 89 as B, 70 ~ 79 as C, 60~69 as D and 0 ~ 59 as E
#include<stdio.h>
int main()
{
float score;
printf("please input the score of the student:\n");
scanf("%f", &score);
if (score>=90 && score <=100)
{
}
else if (score>=80 && score <=89)
{
}
else if (score>=70 && score <=79)
{
}
else if (score>=60 && score <=69)
{
}
else if (score>=0 && score <=59)
{
}
else //That is, the score is > 100 or < 0
{
printf("the score you input is error.\n");
}
return 0;
}
```

#### 4.switch statement

```//The grade feedback range for students is 90 ~ 100 as A, 80 ~ 89 as B, 70 ~ 79 as C, 60~69 as D and 0 ~ 59 as E
#include<stdio.h>
int main()
{
switch(grade) //Match the grade value obtained by switch with the value in each case
{
case 'A': printf("90~100\n"); break; //The value after case is constant
case 'B': printf("80~89\n"); break;
case 'C': printf("70~79\n"); break;
case 'D': printf("60~69\n"); break;
case 'E': printf("0~60\n"); break;
default: printf("the grade you input is wrong.\n");
}
return 0;
}
```

#### 5.while statement

```//Calculate the sum from 1 to 100
#include<stdio.h>
int main()
{
int i = 1, sum = 0;
while (i <= 100) //Judge first and then execute. If the condition of while is false, the loop body executes 0 times
{
sum += i;
i++;
}
printf("%d\n", sum);
return 0;
}
```

#### 6.do ··· while statement

```//Calculate the sum from 1 to 100
#include<stdio.h>
int main()
{
int i = 1, sum = 0;
do
{
sum += i;
i++;
} while (i <= 100); //Execute first and then judge. If the condition of while is false, the loop body executes once
printf("%d\n", sum);
return 0;
}
```

#### 7.for statement

```//Calculate the sum from 1 to 100
#include<stdio.h>
int main()
{
int i , sum = 0;
for (i = 1; i <= 100; i++) //For (initial value of cyclic variable; cyclic condition; increment of cyclic variable)
{
sum += i;
}
printf("%d\n", sum);
return 0;
}
```

#### 8.break statement and continue statement

```//Output a 4 * 5 matrix
#include<stdio.h>
int main()
{
int i , j, n = 0;
for (i = 1; i <= 4; i++) //that 's ok
{
for (j = 1; j <= 5; j++, n++) //column
{
if (n % 5 == 0)
{
printf ("\n"); //Line feed
}
printf("%d\t", i * j);
}
}
return 0;
}
```
##### (1) The break statement terminates the entire loop prematurely
```//Output a 4 * 5 matrix
#include<stdio.h>
int main()
{
int i , j, n = 0;
for (i = 1; i <= 4; i++) //that 's ok
{
for (j = 1; j <= 5; j++, n++) //column
{
if (n % 5 == 0)
{
printf ("\n"); //Line feed
}
if (i == 3 && j == 1)
{
break; //Terminate the entire inner loop without outputting line 3
}
printf("%d\t", i * j);
}
}
return 0;
}
```
##### (2) The continue statement ends the loop ahead of time
```//Output a 4 * 5 matrix
#include<stdio.h>
int main()
{
int i , j, n = 0;
for (i = 1; i <= 4; i++) //that 's ok
{
for (j = 1; j <= 5; j++, n++) //column
{
if (n % 5 == 0)
{
printf ("\n"); //Line feed
}
if (i == 3 && j == 1)
{
continue; //End the inner loop in advance and do not output the original elements in row 3 and column 1
}
printf("%d\t", i * j);
}
}
return 0;
}
```

### (4) Array

#### 1. One dimensional array

```//Bubble sorting
#include<stdio.h>
int main()
{
int a;
int i, j, temp;
for (i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
for (j = 0; j < 9; j++) //9 comparisons in total
{
for (i = 0; i < 9 - i; i++) //Comparison 9-i times per trip
{
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
printf("the sorted numbers:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}

return 0;
}
```
```//Select sort
#include<stdio.h>
int main()
{
int a;
int i, j, temp;
for (i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
for (j = 0; j < 9; j++)
{
for (i = j + 1; i < 10 ; i++) //Compare the number after this number with this number
{
if (a[j] > a[i])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
printf("the sorted numbers:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
```

#### 2. Two dimensional array

```//Matrix transpose
#include<stdio.h>
int main()
{
int a = {{1, 2, 3}, {4, 5, 6}};
int b, i, j;
printf("array a:\n");
for (i = 0; i < 2; i++) //Processing line
{
for (j = 0; j < 3; j++) //Processing column
{
printf("%5d", a[i][j]);
b[j][i] = a[i][j];
}
printf("\n");
}
printf("array b:\n");
for (i = 0; i < 3; i++) //Processing line
{
for (j = 0; j < 2; j++) //Processing column
{
printf("%5d", b[i][j]);
}
printf("\n");
}
return 0;
}
```

#### 3. String array

```#include<stdio.h>
int main()
{
int i, j;
char c1 = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'}; //Define and initialize
for (i = 0; i < 10; i++) //output
{
printf("%c", c1[i]);
}
printf("\n");
char c2 = {{' ', '*', ' '}, {'*', '*', '*'}, {' ', '*', ' '}}; //Define and initialize
for (i = 0; i < 3; i++) //output
{
for (j = 0; j < 3; j++)
{
printf("%c", c2[i][j]);
}
printf("\n");
}
return 0;
}
```
```#include<stdio.h>
#Include < string. H > / / string function header file
int main()
{
char str;
gets(str); //Enter a string within the specified length
puts(str); //Output string
char str1 = {"hello"};
char str2 = {"world"};
printf("%s\n", strcat(str1, str2)); //String 1, 2 splicing
printf("%s\n", strcpy(str1, str2)); //Copy string 2 to 1
char str3 = {"apple"};
char str4 = {"banana"};
if (strcmp(str3, str4) > 0) //Compare string 3 with string 4. Greater than is a positive number, less than is a negative number, equal to 0
{
printf(">\n");
}
if (strcmp(str3, str4) < 0)
{
printf("<\n");
}
if (strcmp(str3, str4) == 0)
{
printf("=\n");
}
printf("%d\n", strlen("helloworld")); //Find string length
return 0;
}
```

### (5) Functions

#### 1. Definition, declaration and call of function, formal parameters and arguments of function

```#include<stdio.h>
int main()
{
int max(int x, int y); //Function declaration
int a, b, c;
scanf("%d%d", &a, &b);
c = max(a, b); //Function call, a and B are arguments
printf("max is: %d\n", c);
return 0;
}
int max(int x, int y) //Function definition, X and y are formal parameters
{
int z;
z = x > y ? x : y;
return z;
}
```

#### 2. Nested call of functions

Call another function in a function.

```#include<stdio.h>
int main()
{
int max4(int a, int b, int c, int d);
int a, b, c, d, max;
scanf("%d%d%d%d", &a, &b, &c, &d);
max = max4(a, b, c, d);
printf("max is: %d\n", max);
return 0;
}
int max2(int x, int y) //Returns the maximum value of x,y
{
int z;
z = x > y ? x : y;
return z;
}
int max4(int a, int b, int c, int d)
{
int max2(int x, int y); //Nested calls to functions
int m;
m = max2(a, b); //Find the maximum value of a and B
m = max2(m, c); //Find the maximum value of a, B and C
m = max2(m, d); //Find the maximum value of a, B, C and D
return m;
}
```

#### 3. Recursive call of function

Call itself within a function.

```//Factorization of n by recursive method
#include<stdio.h>
int main()
{
int fac(int n);
int n, res;
printf("please innput an integer number: ");
scanf("%d", &n);
res = fac(n);
printf("%d! = %d\n", n, res);
return 0;
}
int fac(int n)
{
int f;
if (n < 0)
{
printf("data error\n");
}
else if (n == 0 ||n == 1) //The factorial of both 0 and 1 is 1
{
f =1;
}
else
{
f = fac(n -1) *n; //Recursive call of function
}
return f;
}
```

#### 4. Global and local variables

① Global variables are outside functions and are usually represented in uppercase letters.

② Global variables can act on all functions, and local variables can only act on their functions.

③ When a global variable has the same name as a local variable, the local variable shall prevail.

### (6) Pointer

#### 1. Pointer and pointer variable

Pointer variable: the variable that stores the address. Its value is the address, that is, the pointer

(&: address character; *: value character)

```//Enter two integers a and B, and output a and B in the order of first large and then small
#include<stdio.h>
int main()
{
int a, b, *p1, *p2, *p;
printf("please input 2 ingeter numbers: ");
scanf("%d%d", &a, &b);
p1 = &a; //p1 points to a, that is, assign the address of a to p1
p2 = &b; //p2 points to b, that is, the address of b is assigned to p2
if (a < b) //If a < B, exchange the addresses stored in P1 and P2
{
p = p1;
p1 = p2;
p2 = p;
}
printf("max = %d, min = %d\n", *p1, *p2); //Output the value of the variable indicated by P1 and P2, that is, find the corresponding value through the address stored in P1 and P2
return 0;
}
```

#### 2. Pointer variables as function parameters

```//Enter two integers a and B, and output a and B in the order of first large and then small
#include<stdio.h>
int main()
{
void swap(int *p1, int *p2); //Pointer variables as formal parameters
int a, b, *p1, *p2;
printf("please input 2 ingeter numbers: ");
scanf("%d%d", &a, &b);
p1 = &a;
p2 = &b;
if (a < b)
{
swap(p1, p2); //Pointer variables as arguments
}
printf("max = %d, min = %d\n", *p1, *p2);
return 0;
}
void swap(int *p1, int *p2) //Exchange the value indicated by the pointer
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
```

#### 3. Reference array through pointer

##### (1) Reference a one-dimensional array through a pointer
```//Store the n integers of array a in reverse order
#include<stdio.h>
int main()
{
void inv(int *a, int n);
int i, a = {2, 1, 4, 3, 6, 5, 8, 7, 10, 9};
printf("the original array:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\n");
inv(a, 10);
printf("the inverted array:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
void inv(int *a, int n)
{
int temp;
int *l, *r, *m;
l = a; //l points to the first element of the array
r = a + n - 1; //r points to the tail element of the array
m = a + (n - 1) / 2; //m points to the middle element of the array
for (; l <= m; l++, r--)
{
temp = *l;
*l = * r;
*r = temp;
}
}
```
##### (2) Reference multidimensional array through pointer
```For multidimensional arrays a

a[i] + j ，*(a + i)+j Representative element a[i][j]Address of
*(a[i] + j) ，*(*(a + i)+j)Representative element a[i][j]Value of
```

#### 4. Reference string through pointer

```//Copy the contents of string a to b
#include<stdio.h>
int main()
{
char a = "helloworld", b, *p1, *p2;
p1 = a; //P1 and P2 point to the first element of array a and array b respectively
p2 = b;
for (; *p1 != '\0'; p1++, p2++)
{
*p2 = *p1; //Assign the value of the element indicated by p1 to p2
}
*p2 = '\0'; //After copying all the contents of string a to b, add '\ 0' at the end of b
printf("string a is : %s\n", a);
printf("string b is : %s\n", b);
return 0;
}
```

### (7) Structure

Structure: a combined data structure composed of different types of data.

#### 1. Structural variables

```//Enter the scores of the two students and compare them
#include <stdio.h>
struct Student //Declare structure type struct Student
{
int num;
char name;
float score;
};
int main()
{
struct Student student1, student2; //Define structure variables
scanf("%d%s%f", &student1.num, student1.name, &student1.score); //Initializing structure variables
scanf("%d%s%f", &student2.num, student2.name, &student2.score);
printf("The higher score is:\n");
if (student1.score > student2.score)	//Reference structure variable
{
printf("%d  %s  %6.2f\n", student1.num, student1.name, student1.score);
}
else if (student1.score < student2.score)
{
printf("%d  %s  %6.2f\n", student2.num, student2.name, student2.score);
}
else
{
printf("%d  %s  %6.2f\n", student1.num, student1.name, student1.score);
printf("%d  %s  %6.2f\n", student2.num, student2.name, student2.score);
}
return 0;
}
```

#### 2. Structure array

```//Use the structure array to store the information of n students and sort them according to their grades
#include <stdio.h>
struct Student //Declare structure type struct Student
{
int num;
char name;
float score;
};
int main()
{
struct Student stu = {{101, "zhao", 66}, {102, "qian", 88}, {103, "sun", 77}, {104, "li", 99}};
//Defines and initializes a structure array
struct Student temp;
//Defines structure variables for exchange
const int n = 4;
//Defines the constant variable n, whose value remains unchanged when the program runs
int i, j;
for (i = 0; i < n - 1; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (stu[j].score > stu[i].score)
{
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
printf("The order is:\n");
for (i = 0; i < n; i++)
{
printf("%6d%8s%6.2f\n", stu[i].num, stu[i].name, stu[i].score);
}
return 0;
}
```

#### 3. Structure pointer

##### (1) Pointer to structure variable
```//Members in a structure variable are accessed through a pointer to the structure variable
#include <stdio.h>
struct Student //Declare structure type struct Student
{
int num;
char name;
float score;
} student = {101, "zhao",88}; //Define and initialize the structure variable student
int main()
{
struct Student *p;	//Defines a pointer to a structure variable
p = &student;
printf("%6d%8s%6.2f\n", (*p).num, (*p).name, (*p).score); //Accessing members in structure variables through pointers
return 0;
}
```
##### (2) Pointer to structure array
```//Through the pointer to the structure array, all the information of students in the structure array with student information is output
#include <stdio.h>
struct Student //Declare structure type struct Student
{
int num;
char name;
float score;
} stu = {{101, "zhao", 88}, {102, "qian", 77}, {103, "sun", 99}}; //Define and initialize the struct array stu
int main()
{
struct Student *p;              //Defines a pointer to an array of structures
for (p = stu; p < stu + 3; p++) //Stu is the address of the first element in the stu array
{
printf("%6d%8s%6.2f\n", p->num, p->name, p->score); //Output the information of the elements in the structure array through the pointer
}
}
```
##### (3) Structure variables and pointers to structure variables are used as function parameters
```//Input the scores of N students and output the information of the students with the highest scores, which are realized by three functions input, Max and print respectively
#include <stdio.h>
#define N 3 / / define N=5 with Symbolic Constant
struct Student
{
int num;
char name;
float score;
};
int main()
{
void input(struct Student stu[]);
struct Student max(struct Student stu[]);
void print(struct Student stud);
struct Student stu[N];   //Define structure array
struct Student *p = stu; //Defines a pointer to an array of structures
input(p);
print(max(p)); //Take the structure variable returned by the max function as the argument of the function
return 0;
}
void input(struct Student stu[]) //Take the structure array as the formal parameter of the function
{
int i;
printf("Please input the number,name and score of the students:\n");
for (i = 0; i < N; ++i)
{
scanf("%d%s%f", &stu[i].num, stu[i].name, &stu[i].score);
}
}
struct Student max(struct Student stu[]) //Take the structure array as the formal parameter of the function
{
int i, m = 0;
for (i = 0; i < N; ++i)
{
if (stu[i].score > stu[m].score)
{
m = i; //Use m to store the serial number of the student with the highest score in the array
}
}
return stu[m]; //Returns the struct variable of the student with the highest score
}
void print(struct Student stud) //Structure variables as function parameters
{
printf("The highest score student is:\n");
printf("%6d%8s%6.2f\n", stud.num, stud.name, stud.score);
}
```

#### 4. Handle linked list with pointer

Linked list: a structure that dynamically allocates storage.

The head pointer of the linked list stores an address, which points to an element. Each element in the linked list is called a node. Each node contains two parts, data and the address of the next node. The head points to the first element, and the first element points to the second element... Until the last element, the element no longer points to other elements, which is called the tail of the list, and its address is stored "NULL" (empty address), the linked list ends here.

Let p = head and continuously execute statements P = P - > next to p = NULL to traverse the entire linked list.

##### (1) Establishment and output of static linked list
```//Establish a static linked list, which is composed of three student information nodes, and output the data of each node
#include <stdio.h>
struct Student
{
int num;
float score;
struct Student *next;
};
int main()
{
struct Student a, b, c, *head, *p; //Three structural variables a, B and C are defined as the nodes of the linked list
a.num = 101;
a.score = 88;
b.num = 102;
b.score = 77;
c.num = 103;
c.score = 99;
a.next = &b;            //The next member of node a stores the address of node b
b.next = &c;            //The next member of node b stores the address of node c
c.next = NULL;          //The next member of node c does not store the addresses of other nodes
while (p != NULL) //The loop ends when p stores an empty address
{
printf("%6d%6.2f\n", p->num, p->score);
p = p->next; //Assign the address pointed to by the next member of p to p
}
return 0;
}
```
##### (2) Establishment and output of dynamic linked list
```//Establish a dynamic linked list that can input multiple student information (end with input of 0.0) and output it
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct Student) / / use LEN to represent the length of struct Student type data
struct Student
{
int num;
float score;
struct Student *next;
};
int n;                  //Count the number of linked list nodes with n
struct Student *creat() //Create a linked list function that returns a header pointer
{
struct Student *p1, *p2;
n = 0;
p1 = p2 = (struct Student *)malloc(LEN); //Open the first node and point P1 and P2 to it
scanf("%d%f", &p1->num, &p1->score);     //Input the information of the first student at the first node indicated by p1
head = NULL;                             //First, make the value of head NULL, that is, head does not point to any node
while (p1->num != 0)                     //When the student number entered is zero, the cycle ends
{
n = n + 1;  //Add one to the number of links in the list
if (n == 1) //When it is the first node
{
head = p1; //Assign the value of p1 to the head, even if the head also points to the first node opened, so that the node becomes the first node in the linked list
}
else
{
p2->next = p1; //Assign the value of p1 to p2 - > next. Since p2 points to the previous node, execute this statement to connect the newly established node to the previous node
}
p2 = p1;                             //Make p2 point to the newly established node
p1 = (struct Student *)malloc(LEN);  //Open up a new node and point p1 to it
scanf("%d%f", &p1->num, &p1->score); //Input the information of the next student at the new node indicated by p1
}
p2->next = NULL; //Assign NULL to P2 - > next, and the table creation process ends. Due to the end of the cycle, even if p1 points to the newly opened node, it cannot be linked into the linked list
}
{
struct Student *p;
printf("These %d records are:\n", n);
p = head;         //Make p point to the first node
if (head != NULL) //If it is not an empty table
{
while (p != NULL) //If p is not an empty address
{
printf("%6d%6.2f\n", p->num, p->score);
p = p->next; //Make p point to the next node
}
}
}
int main()
{