C language final exam knowledge points summary

Table of contents

1. General review of C language

2. Chapter overview

3. Commonly used program source code

1. General review of C language

In general it must be clear that:

1) There are three types of program structure:  sequence structure  ,  loop structure  (three loop structures),  selection structure  (if  and  switch)

2) Read the program from the main () entry,  and then read from the top to the bottom (when encountering a loop, do a loop, and when encountering a selection, do a selection) until the end of the main function.

3) The data of the computer is stored in the computer in binary form, and the location where the data is stored is its address, and the address is in hexadecimal.

4) bit means a bit means 0  or 1.  byte  means byte,  one byte  =  eight bits.

5) Be sure to remember how binary is divided into decimal.

6) There are three common ways to describe algorithms: pseudocode, flowchart or NS diagram, and natural sentences.

Concepts are often considered: 

1. Compiler preprocessing is not part of the C language and is no longer run-time. The program compiled in C language is called the source program, which is stored in the text file as ASCII value.

2. There is one and only one main function in each C language program.

3. Functions cannot be defined in functions.

4. The algorithm must have an output, and it may not have an input.

5. break can be used in loop structures and switch statements.

6. Among the operator priorities, unary operator>binary operator>ternary operator, arithmetic operator>relational operator>logical operator>assignment operator, comma operator has the lowest level.

2. Chapter overview

Chapter One

1) Legal user identifier inspection:

Valid requirements are letters, numbers, and underscores. It is wrong to have other elements.

And the first must be a letter or an underscore. The first number is wrong.

Keywords cannot be used as user identifiers. Neither main define scanf  nor printf are keywords. What confuses you is that it can be used as a user identifier. Because the first letter in If is capitalized, it is not a keyword.

2) The legal form of real data:

2.333e-1  is legal, and the data is 2.333×10-1.

Exam formula: there must be a number before e and after e, and there must be an integer after e. .

3) Legal forms of character data::

   '1'  means a character occupies one byte, and "1" means a string occupies two bytes (with an end symbol).

 The ASCII value of '0'  is 48,  the ASCII value of 'a' is 97, and the ASCII value of 'A' is 65.

4) A character variable occupies 1 byte, an integer occupies 4 bytes, a single-precision floating-point variable occupies 4 bytes, and a double-precision variable occupies 8 bytes.

5) Examination of escape characters:

   

escape character effect ASCII value
\a ring the bell 007
\b backspace 008
\f change page 012
\n new line 010
\r carriage return 013
\t horizontal tab 009
\v vertical tabulation 011
\\ backslash 092
\? question mark character 063
\' single quote character 039
\" double quote character 034
\0 null character 000
\ddd any character three-digit octal
\xhh any character binary hexadecimal

6) Priority levels of arithmetic operation symbols:

   Some of the same level are from left to right, and some are from right to left.

7) Mandatory type conversion:

  It must be (int)a  not int(a). Note that there must be parentheses on the type.

   Note  the difference between (int)(a+b) and (int)a+b. The former is to transform a+b, and the latter is to transform a and add b.

8) Examination of expressions:

   An expression must have a value.

   Assignment expression : the value of the expression is the leftmost value, a=b=5; the expression is 5, and the constant cannot be assigned.

   Self-increment and self-decrement expressions: Assume a=5, ++a (is 6),  a++ (is 5);

The mechanism of operation: ++a  is to add 1 to the value of the variable first, then put the obtained value into the variable a, and then use this ++a expression to obtain a value of 6 , and a++ uses this expression first The value of a is 5 , and then add 1 to the value of a to get 6,   

Then put it in the variable a. After performing ++a and a++, if a is used in the following program, it will be 6 in the variable a.

  Exam formula: ++ is added before use, ++ is used first before adding.

Comma expression : the lowest priority; the value of the expression is the value of the expression to the right of the comma.

The expression for (2, 3, 4) evaluates to 4.

9) Examination of bit operations:

 There will be one or two exam questions.

General processing method: Almost all bit operations must be handled according to this process (first convert decimal to binary and then to decimal).

Example 1: char a = 6, b;

b = a<<2; The calculation of this kind of problem is to first convert the decimal 6 of a into binary, and then do bit operations.

Example 2: It is important to remember that

Example 3: When the data is not discarded, <<shift one bit to the left means multiply by 2; >>shift one bit to the right means divide by 2.

10) The value of 018 is illegal, there is no 8 in octal, every 8 enters 1. 

11) Both sides of the % symbol are required to be integers. It is wrong if it is not an integer.

12) When rounding and losing decimals:

       1、int a =1.6;

              2、(int)a;  

Chapter two

1) Check the format of the printf function:

     %d corresponds to integer); %c corresponds to character; %f corresponds to single precision and so on. Width, left alignment, etc. modifiers.

     %ld corresponds to  long int; %lf  corresponds to double.

Note: Sometimes it is necessary to use forms such as: %3d, %.f, %-5, etc. The numbers before '%' and 'd/c/f' represent how many positions the output data occupies, and the negative numbers before the numbers The sign indicates that the output data should be more aligned, and this method can well achieve the beauty of the output data.

2) Format inspection of scanf function:

   Note that the second part of the function is an address like &a , not a ;     

   Scanf("%d%d%*d%d",&a,&b,&c);  skip the third input data.

Note: If each data is separated by a space, the space before %c cannot be omitted, otherwise an error will occur.

3) Examination of putchar and getchar  functions:

   char a = getchar()  has no parameters, and gets a character you input from the keyboard to the variable a.

   putchar('y') outputs the character y to the screen.

4) How to realize  the exchange of the values ​​in the two variables x and y (required to memorize them)

   It is not possible to put  x=y, y=x;  use the intermediate variable  t=x; x=y; y=t.

5) How to realize the program of retaining three decimal places and rounding the fourth place (required to memorize)

   This has the significance of promotion. Note that  x =  (int)x  removes the decimal part.

third chapter

Special attention should be paid to: In the C language, non-zero is used to indicate logical true, and 0 is used to indicate logical false.  

1) Relational expression:

   The value of the expression can only be 1 (meaning true), or 0 (meaning false)

   Gets 1 when the expression of the relation is true. For example,  9>8 is true, so the value of the expression is 1;

2) Logical expression:

     Can only be 1 (meaning true), or 0 (meaning false)

     a) Total && ||    ! Three logical operation symbols.

     b)! >&&>|| Priority levels.

     c) Pay attention to the short circuit phenomenon. I like to pass the exam.

     d) To indicate that  x  is greater than 0 and less than 10. 0<x<10 is not allowed (be sure to remember). The result of calculating 0<x first is 1 or 0; then comparing 0 or 1 with 10 is always true (1). So be sure to use (0<x)&&(x<10) to indicate that it is larger than 0 and smaller than 10.    

3) if  statement

   The else  is combined with the closest if without the else.

4) Conditional expression:

    expression 1  ? expression2  : expression3

    Note that when it is not 0 , it is the value of expression 2, and when it is 0 , it is the value of expression 2.  

Exam formula: true before false.

5) switch statement:

a) Be sure to pay attention to  the difference between having a break and not having a break. When there is no break, as long as one case matches, the rest will be executed. If there is a break, it will directly jump out of the switche statement.

 b) switch can only be used with break, not continue.

Chapter Four

1) Three loop structures:

   a)for() ; while();   do- while()三种。

   b) There must be two semicolons in the for loop, don't forget.

   c) When writing a program, you must pay attention to the condition that the loop must end, otherwise it will become an infinite loop.

   d) The semicolon of the last while(); of the do-while() loop must not be lost. (Beware of correcting mistakes on the computer)

2)  The difference between break and continue

   Memory method:

break: It means to break, (to break the whole cycle) so when you see break, you will exit a layer of cycle.

continue: It means to continue, (to continue the loop operation), but to end this loop, the remaining statements in the loop body will not be executed, jump to the beginning of the loop, and then judge the loop condition to start a new round of loop.

3) Nested loops

   Nested use of multiple or multiple loops, there are loops within the loop, the outer loop is executed once, and the inner loop is executed once.

4) The difference between while ((c=getchar()) !='\n') and  while (c=getchar() !='\n')

 First look at the difference between a = 3 != 2 and (a=3)!=2 :

(The level of the != sign is higher than the = sign  , so the first one calculates 3!=2) The value of the first a is the obtained 1; the value of the second a is 3.  

Exam note: The importance of parentheses here. 

chapter Five

Function: It is a program block with certain functions;

1)  The parameters of the function, the return value (schematic diagram):

  main()

{

int a = 5,b=6,c;

  c = add(a,b);

  printf(“%d”,c);

}

Call functions

a, b are actual parameters

The whole function gets a value that is

The return value of the Add function.

int add ( int x, int y)

{

int z;

  z=x+y;

  return z;

}

called function

x, y are formal parameters

The function return value is an integer

z is the result of the calculation of the add function, which is the return value returned by the function to the main program.

The program is executed sequentially from top to bottom. When the function add is encountered, the values ​​of a and b are passed to the calling function, and the program is temporarily interrupted to wait for the value to be returned. After getting the return value, execute it sequentially

2) Be sure to pay attention to the transfer between parameters

  The difference between passing values ​​between actual parameters and formal parameters  , and passing addresses. (point of the exam) 

      If the value is passed, the change of the formal parameter will not change the change of the actual parameter.

      If the address is passed, the change of the formal parameter may change the change of the actual parameter.

3) Examination of function declaration:

Must have: function name, function return type, function parameter type.

Doesn't have to be: the name of the parameter.

4) Recursion: A function calling itself is called recursion, while two functions calling each other is called indirect recursion.

Use recursion carefully: If you are not proficient in using recursion, it is best not to use it, otherwise it is easy to make yourself dizzy, and recursive operations take up a lot of time and space.

Chapter Six

1 Important concepts of one-dimensional arrays:

Discussion of the array a[10].

1. a represents the array name, which is the address of the first element, that is, the address of element a[0].

2. As an array name, a is essentially an address, so as long as a++ appears, or a=a+2 assignment is wrong.

3. a is the name of a one-dimensional array, so it is a column pointer, that is to say, a+1 is to jump a column. 

Discussion of a[3][3].

1. a represents the array name, which is the address of the first element, that is, the address of the element a[3][3].

2. As an array name, a is essentially an address, so as long as a++ appears, or a=a+2 assignment is wrong.

3. a is the name of a two-dimensional array, so it is a row pointer, that is to say, a+1 is to skip a row.

4. a[0], a[1], and a[2] are also address constants, which cannot be assigned. At the same time, they are all column pointers, a[0]+1, a[1]+1 , a[2]+1 is to skip a column.

5. Note that a  is different from a[0], a[1] and a[2], and their base types are different. The former is a row of elements, and the latter three are a column of elements.

Techniques for two-dimensional arrays:

If there is a topic like a[3][3]={1,2,3,4,5,6,7,8,9}.

Step 1: Write them in matrix form:      

first column second column third column  

a[0]à  1      2       3    -> the first row

a[1]à 4      5   6  —> second row

a[2]à 7     8       9 -> the third line

Step 2: It is very simple to do this between the questions:    

*(a[0]+1) We know that the first element of the first row jumps one column behind, then here is the a[0][1] element, so it is 2.

*(a[1]+2) We know that the first element of the second row jumps two columns back. Then here is the a[1][2] element, so it is 6.

Be sure to remember: as long as it is a two-dimensional array topic, it must be written in the above format, and then do the topic, it will be easier.

Array initialization, one-dimensional and two-dimensional, one-dimensional can not be written, two-dimensional second must be written

      int a[] = {1, 2}  legal. int a[][4] = {2, 3, 4} is legal.   But int a[4][]={2, 3, 4} is illegal.

row pointer int a[1][2] in a two-dimensional array        ; 

Among them, a is now a row pointer, and a+1 jumps a row of array elements. With (*) p[2] pointer

     a[0], a[1] are now a column pointer. a[0]+1  jumps an array element. Use with *p[2] pointer array

And remember the rules of undressing:

   a[2] becomes *(a+2) a[2][3] becomes  *(a+2)[3] and then can become *(*(a+2)+3)    This idea is very important!

Chapter VII

The essence of pointer variables is to store addresses, while ordinary variables are used to store values. 

 The difference between *p and p in int *p :

*p can be used as a variable; the function of * is to take the value in the following address p

p is used as an address.

 The difference between *p++ and (*p)++: important in correcting mistakes

         *p++ means that the address will change.

         (*p)++  means that the value will change.                  

Note: Clearly distinguish pointer arrays, array pointers, pointer functions and function pointers.

To put it more simply: the four things "pointer array, array pointer, pointer function, and function pointer" can be understood in words—for example, "array of pointers", it will be clear after a little modification of the text. In this way, the front is modification; and the back of "of" is structure. It's easy to understand.

Function pointer: It is a pointer to a function - it is a pointer itself, and stores the entry address of a function

int (*fun)(int x);

Pointer function: It is essentially a function whose return value is a pointer

int *fun(int x);

Array pointer: It is essentially a pointer to an array, which stores the address of the array head element

int (*arr)[10];

Pointer array : refers to an array containing pointers - essentially an array, but each element of this array is a pointer

int *a[10];

other:

Three principles: (the focus of the exam)

   Array name: indicates the address of the first element. The array name cannot be added by itself, it is the address constant name. (tested many times)

   Function name: Indicates the entry address of the function.

   String constant name: indicates the address of the first character.

Note: 1). The array subscript should not exceed the bounds;

                  2). The return type of the function is determined by the function type rather than the variable type;

                  3). The operator priority must be clearly remembered;

                  4). It is best to have comments when writing a program. This is not only a good habit, but also helps the program to look beautiful and increase the readability of the program.

3. Commonly used program source code


Bubble sorting (in order to keep the program simple and take care of readers whose functions may not be clear, the three basic sorting programs do not use custom functions):

#define _CRT_SECURE_NO_WARNINGS
#define N 10
#include <stdio.h>
void main()
{
	int a[N], i, j, t;
	for (i = 0; i < N; i++)                //输入N个数据
	{
		scanf("%d", &a[i]);
	}
	for (i = 0; i < N-1; i++)              //外层循环控制轮数,只需要进行N-1轮
	{
		for (j = 0; j < N - 1 - i; j++)    //内层循环遍历后面每个数据(可以理解为数学中的握手问题,10数据分别需要9,8,7...2,1次。)
		{
			if (a[j] > a[j+1])             //大小判断
			{
				t = a[j];                  //数据置换
				a[j] = a[j+1];
				a[j+1] = t;
			}
		}
	}
	for (i = 0; i < N; i++)               //输出
	{
		printf("%-3d", a[i]);
	}
}

Select sort:

#define _CRT_SECURE_NO_WARNINGS
#define N 10                           //定义N为10,即数组的长度
#include <stdio.h>
void main()
{
	int a[N] , t, i,j;
	for (i = 0; i < N; i++)            //循环输入N个数据
		scanf("%d", &a[i]);            
	for (i = 0; i < N - 1; i++)        //选择排序需要进行N-1轮比较,外层循环控制轮数
	{
		for (j = i + 1; j < N; j++)    //因为是a[i]同它之后的数据作比较,所以j从i+1开始,内层循环让a[i]同它之后的每个数据进行比较
		{
			if (a[i] < a[j])           //判断大小
			{     
				t = a[i];              //数据置换
				a[i] = a[j];
				a[j] = t;
			}
		}
	}
	for (i = 0; i < N; i++)            //输出
	{
		printf("%6d", a[i]);
	}
}

Insertion sort:

#define _CRT_SECURE_NO_WARNINGS
#define N 10
#include <stdio.h>
void main()
{
	int i, j, t,a[N];
	for (i = 0; i < N; i++)         //输入N个数据
		scanf("%d", &a[i]);
	for (i = 1; i < N; i++)         //外层循环从第二数据开始
	{
		t = a[i];                   
		j = i-1;                    //j从0开始
		while (j >= 0 && a[j] > t)  //判断大小并且保证下标不会越界
		{
            a[j + 1] = a[j];        //数据的置换
            j--;
		}
        a[j + 1] = t;               
	}
	for (i = 0; i < N; i++)        //循环输出
		printf("%-3d", a[i]);
}

Snake matrix:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void fun(int N, int r)               //形参N,r
{
	int f = 1, i;
	for (i = 1; i <= r; i++)
	{
		f += i - 1;                   //f为每一行第一个数据的值
	}
	int t = f;
	int n = r + 1;
	for (i = 1; i < N - r + 1; i++)
	{
		if (i < N - r + 1)
			printf("%d ", t);         //不是每行最后一个数据则多数出一个空格
		else
			printf("%d", t);          //每行最红一个空格只输出数据
		t += n;                       //t存放每次要输出的数据
		n++;                          //n每次循环后自加
	}
}
void main()
{
	int N, i;
	scanf("%d", &N);                 //输入行数
	for (i = 1; i <= N; i++)         //循环控制每行
	{
		fun(N, i);                   //实参对应自定义函数N,r
		printf("\n");                //每行输出结束后换行
	}
}

Fibonacci of function recursion:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int Fib(int n)
{
	if (n == 1 || n == 2)                     //斐波那契数列的第一二项均为1
		return 1;
	else
		return Fib(n - 1) + Fib(n - 2);      //递归调用
}
void main()
{
	int n,s;          
	scanf("%d", &n);                         //输入n
	s = Fib(n);                              //s存储Fib函数的返回值
	printf("%d\n", s);
}

Tower of Hanoi with function recursion:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void fun(int n, int a, int b, int c)
{
	if (n == 1)
		printf("%d->%d\n", a, c);
	else
	{
		fun(n - 1, a, c, b);
		printf("%d->%d\n", a, c);
		fun(n - 1, b, a, c);
	}
}
void main()
{
	int n;
	scanf("%d", &n);
	fun(n, 1, 2, 3);
}

Finally, I attach a confession program that I copied, I wish you all get out of the order as soon as possible, grasp it:

#include<stdio.h>
#include<math.h> 
#include<windows.h>
int main() {
	printf("\t\t\t\t\t**我爱你\n\n");
	printf("\t\t\t\t绸缪束薪,\t\t三心在天。\n");
	printf("\t\t\t\t今夕何夕,\t\t见此良人。\n");
	printf("\t\t\t\t子兮子兮,\t\t如此良人何!\n");
	for (double y = 1.9; y > -1.2; y -= 0.1) {
		for (double x = -1.8; x < 2.6; x += 0.04) {
			if (x * x + (y - pow(x * x, 1.0 / 3)) * (y - pow(x * x, 1.0 / 3)) <= 1)	printf("*");
			else if (x <= -1.4 && x >= -1.7 && y <= 1.6 && y >= -1.1)			    printf("*");
			else if (x >= 1.4 && x <= 1.6 && y <= 1.6 && y >= -1.1)			        printf("*");
			else if (x >= 2.3 && x <= 2.5 && y <= 1.6 && y >= -1.1)			        printf("*");
			else if (x >= 1.6 && x <= 2.3 && y <= -0.7 && y >= -1.1)			    printf("*");
			else	printf(" ");
		}
		system("color 5c");
		printf("\n");
	}
	getchar();
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_53811934/article/details/121712071