Summary of basic knowledge of C language (recommended collection)

Foreword: C language is the necessary basic knowledge in the study and life of contemporary people. It is widely used. The following is a summary of the basic knowledge of C language. It is definitely not a fantasy to get started with zero basic knowledge of C language!

1 Algorithm structure

1. Sequence structure, selection structure, loop structure; 2. Loop structure is divided into while type, until type, for loop structure; program flow chart;

2 Structured programming methods:

(1) Top-down; (2) Gradual refinement; (3) Modular design; (4) Structured coding.

3 data types:

Constants: Constants include literal constants, direct constants and symbolic constants;

Variables: The C language stipulates that identifiers can only be composed of three characters: letters, numbers and underscores, and the first character must be a letter or an underscore; it must be defined first and then used; after each variable is defined to determine the type, it is compiled The corresponding storage unit can be allocated for it;

Integer type: Integer constants have decimal, octal, and hexadecimal; '%d'

Integer variable: the storage form of data in memory is stored in binary form; there are int type, short int type and long int type, the range of unsigned integer variable is -32768—32767, and the signed type is 0~65535. Long is defined as 32 bits, short is defined as 16 bits, and int can be 32 bits or 16 bits, which mainly depends on the machine word length.

Representation of real constants: (1) Decimal, 0.0; (2) Exponential, 123e3

Real variable: the storage form of real data in memory, generally occupying 4 bytes in memory, divided into integer part and decimal part for storage. Real variable is divided into float type, double type long double type. Real data will have rounding errors.

Types of real constants: The C compiler system treats real constants as double precision.

Character array: (1) Character constants: escape character (\n—newline, \t—tab,\r—carriage return, \f—form feed, \b—backspace, \ddd— -1 to 3 characters represented by octal numbers)
(2) Character variable: the storage form of character data is actually stored in ASCII code. "%c"
string constant: A sequence of characters enclosed in double quotes.
insert image description here

4 C operators are as follows:

1. Arithmetic operators (+ - * / %) combine direction from left to right
2. Relational operators (> < == "">= <= !="">
3. Logical operators (! && ||)
4. Bitwise operator (<>> ~ | ^ &)
5. Assignment operator (= and sign extension assignment operator)
6. Conditional operator (? : )
7. Comma operator ( , )
8. Pointer operator (* &)
9. Byte operator (sizeof)
10. Mandatory conversion operator ((type))
11. Component operator ( . ->)
12. Subscript operator ([])
13. Others

5 Control Statements:

Complete certain control functions.
1. if() else
2. for()~
3. while()~
4. do~while()
5. continue
6. break
7. switch
8. goto
9. return

6 character data input and output:

1. putchar() input character variable
2. getchar() can only accept one character

Format input and output:

1. printf (%d—integer, %c—character, %ld, %md, %o, %u, %s, %-m.nf, %e, %g) 2. scanf (format control
, address list)insert image description here

array

Definition of one-dimensional array: type specifier array name [constant expression]; define first and then reference; when initializing a one-dimensional array, only a part of elements can be initialized, and when all array elements are initialized, the length can be specified; When the defined array length is different from the provided initial value, the array length cannot be omitted.

The definition of two-dimensional array: type specifier array name [constant expression] [constant expression] in C language, the storage of two-dimensional array is to store the elements of the first line first, followed by the second line, in fact, it is also one-dimensional way to store. If the initial values ​​of all elements can be specified during initialization, the size of the first dimension can be omitted, but the second dimension cannot be omitted.

Character array: The definition and initialization are similar to arrays, but single quotes are required. Character and character string end mark, specified by C language, represented by '\0'.

1. puts() outputs a string to the terminal
2. gets() inputs a string from the terminal to a character array and gets a function value.
3. strcat() concatenates strings in two character arrays.
4, strcpy () string copy function.
5. strcmp() compares strings.
6. The function of strlen() to test the length of the string does not include "\0"
7. strlwr() converts the uppercase letters in the string to lowercase letters.
8. strupr() converts lowercase letters in a string to uppercase letters.

function

(1) A source program consists of multiple functions.
(2) The execution of the C program starts from the main() function;
(3) All functions are parallel;
(4) Function classification; can be divided into standard and custom, and can also be divided into functions with parameters and functions without parameters.

The general form of a function definition:

(1) type designator function name ()
{ declaration part statement } (2) type designator function name (formal parameter list) { declaration part statement }







Explanation on formal parameters and actual parameters:

(1) The formal parameters specified in the definition function do not occupy the storage unit in the memory when there is no function call, and the memory will be allocated only when the call occurs.
(2) The actual parameter can be a constant, variable or expression; sometimes the address is passed;
(3) In being defined, the formal parameter must specify the type;
(4) The type of the actual participating formal parameter should be the same or assignment compatible;
( 5) The C language stipulates that the data transfer between the actual parameter variable and the formal parameter variable is "value transfer", that is, one-way transfer, only the actual parameter is passed to the formal parameter, and the formal parameter cannot be passed to the actual parameter.

The return value of the function:

It is hoped that the calling function can get a definite value through the function call.
(1) The return value of the function is obtained through the return statement in the function.
(2) The type of the function value;
(3) If the type of the function value is different from the value of the expression in the return statement, the function type shall prevail.
(4) If there is no return statement in the calling function, it does not return a certain value required by the user, the function does not return a value, but just does not return a useful value, and returns an uncertain value.
(5) If you do not need to bring back any value, use void.

Function call:

Call method 1. Function statement; 2. Function expression; 3. Function parameter.

Declaration of the function being called:

Conditions for a function to call another function:

1. The function to be called first must be an existing function;
2. If library functions are used, generally the #include command should be used at the beginning of this file to "include" the information needed to call the relevant library functions into this file middle. .h file is the suffix used for header files.
3. If you use a user-defined function, and the function is in the same file as the function that uses it, you should generally declare the called function in the calling function.
4. If the definition of the called function appears before the calling function, there is no need to declare it.
5. If function declarations have been made outside the functions before all functions are defined, there is no need to declare more functions to be called in each calling function.
insert image description here

Local and global variables:

(1) Local variables Variables defined inside a function are internal variables, which are only valid within the scope of this function, and the main function cannot use variables defined in other functions; variables with the same name can be used in different functions, and they Represents different objects without interfering with each other; formal parameters are also local variables; inside a function, variables can be defined in compound statements, and these variables are only valid in this compound statement. This kind of compound statement can also be called "subprogram" or "program block";

(2) Global variables. Variables defined outside functions are called external variables. Global variables can increase the channel for data connection between functions. Generally, they should not be used when they are no longer necessary. They occupy storage units during the entire execution of the program. The generality of the function, the use of global variables will reduce the clarity of the program. Also note that if the external variable and the local variable have the same name in the same source file, the external variable is "shielded" within the scope of the local variable and has no effect.

Variable storage class:

(Variable value storage time) dynamic storage method, which is divided into dynamic storage space during program running, static storage method refers to the way of allocating fixed storage space during program running; storage space is divided into program area, static storage area and dynamic storage area; all global variables are placed in the static storage area, the space is allocated at the beginning of the program, and released when the program is finished; the following data is stored in the dynamic storage area:

1. Function formal parameters;

2. Automatic variables;

3. Field protection and return address when the function is called; in C language, each variable and function has two attributes, which are data type and data storage type, and the storage type is the way data is stored in memory.

Storage methods are divided into static and dynamic storage types, including four types: automatic (auto), static (static), register (register), external (extern), if not declared, it will be considered as auto The type will automatically allocate storage space, which belongs to the dynamic storage method.

Static declares that the local variable does not disappear after the function call ends and retains the original value, that is, the occupied storage unit is not released. When the function is called next time, the variable already has a value, which is the value at the end of the last function call. What needs to be explained is that if the local variable is defined without an initial value, the static local variable is automatically assigned a value of 0 or a null character when compiling. Although the static local variable still exists after the function call ends, other functions cannot refer to it. Static local variables are mainly used when the variable is only referenced without changing its value after initialization.

The Register variable is a C language that allows the value of a local variable to be placed in a register in the CPU. When needed, it is directly taken out of the register to participate in the operation, and it does not need to be extracted from the memory. However, the number of registers in the computer system is limited, and it cannot be defined arbitrarily. memory, local static variables cannot be defined as register variables.

Extern declares external variables to extend the scope of external variables. In a file, if the function before the definition wants to refer to the external variable, the keyword extern should be used to declare the variable as an external variable before the reference. In multiple files, the method of extern declaration can also be used to declare external variables. Sometimes it is hoped that some local variables can only be referenced by this file, but not by other files. At this time, you can add a static when defining external variables, which strengthens the versatility in the program modular design.

Static has two functions for declaring a variable, one is when declaring a local variable. The space allocated for this variable always exists during the entire program execution; one is in the declaration of the global variable, and the scope of the variable is limited to the operation of this file module.

Note: These methods also apply to function declarations

Guess you like

Origin blog.csdn.net/C214574728/article/details/126976162