Home > Common Problem > body text

Summary of basic knowledge points of C language

爱喝马黛茶的安东尼
Release: 2019-10-26 09:47:18
Original
22565 people have browsed it

C language is an essential basic knowledge for contemporary people's study and life. It is widely used. The following is a summary of the basic knowledge of C language.

Algorithm structure:

1. Sequential structure, selection structure, loop structure; 2. Loop structure is divided into while type, until type, and for loop structure; program Flow chart;

Structured programming method:

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

Data type:

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

Variables: C language stipulates that identifiers can only be It consists of three characters: letters, numbers and underscores, and the first character must be a letter or underscore; it must be defined before use; after each variable is defined to determine the type, the corresponding storage unit can be allocated to it at compile time ;

Integer type: integer constants include decimal, octal and hexadecimal; "%d"

Integer variable: the data is stored in binary form in memory; there is 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 usually defined as 32 bits, short is defined as 16 bits, int can be 32 bits It can also be 16 bits, which mainly depends on the machine word length.

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

Real type variables: The storage form of real number data in memory, usually in memory It occupies 4 bytes and is divided into integer part and decimal part for storage. Real type variables are divided into float type, double type and long double type. Real data will have rounding errors.

Type of real constants: The C compilation system handles real constants as double precision.

Character array: (1) Character constants: escape characters (——line feed,——tab,——carriage return,——page feed,——backspace, ddd——1 to 3 digits Characters represented by octal numbers)

(2) Character variables: The character data storage format is actually stored in ASCII code. "%c"

String constant: a sequence of characters enclosed in double apostrophes.

Related recommendations: "php video tutorial"

The operators of C include the following types:

1. Arithmetic The combination direction of operators (- * / %) is from left to right

2. Relational operators (> < == >= <= !=)

3. Logic Operator (! && ||)

4, bitwise operator (<< >> ~ | ^ &)

5, assignment operator (= and sign extended assignment Operator)

6, conditional operator (? : )

7, comma operator ( , )

8, pointer operator (* &)

9. Seek byte operator (sizeof)

10. Forced type conversion operator ((type))

11. Component operator (. ->)

12. Subscript operator ([])

13. Other

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

Input and output of character data:

1. putchar() input character variable

2. getchar() can only accept one character

Format input and output:

1. printf (%d—integer type, %c—character type, %ld, %md, %o, %u, %s, %-m .nf, %e, %g)

2. scanf (format control, address list)

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 the elements can be initialized, and the length can be specified when initializing all array elements; but if the length of the defined array is different from the provided When the initial values ​​of are different, the array length cannot be omitted.

Definition of two-dimensional array: type specifier array name [constant expression] [constant expression] To store a two-dimensional array in C language, the elements of the first row are stored first, followed by the second row. In fact, it is also stored in a one-dimensional manner. If the initial values ​​of all elements can be specified during initialization, the first dimension size can be omitted, but the second dimension cannot be omitted.

Character array: The definition and initialization are similar to those of arrays, except that single quotes are required. Character and string end marks, specified by C language, are represented by ‘’.

String processing function:

1. puts() outputs a string to the terminal

2. gets() inputs a string from the terminal String to character array and get a function value.

3. strcat() links strings in two character arrays.

4. strcpy() string copy function.

5. strcmp() compares strings.

6. The strlen() function to test the length of a string does not include ""

7. strlwr() converts uppercase letters in the string to lowercase letters.

8. strupr() converts lowercase letters in the 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 For standard and custom functions, they can also be divided into parameterized functions and parameterless functions.

The general form of function definition:

(1) Type identifier function name ()

{
声明部分
语句
}
Copy after login
Copy after login

(2) Type identifier function name ( Formal parameter list)

{
声明部分
语句
}
Copy after login
Copy after login

Explanation on formal parameters and actual parameters:

(1) Formal parameters specified in the definition function, when there is no function call , they do not occupy storage units in memory, and memory is allocated only when a call occurs.

(2) The actual parameter can be a constant, variable or expression; sometimes the address is passed;

(3) In the definition, the formal parameter must specify the type;

(4) The types of actual and formal parameters should be the same or assignment-compatible;

(5) C language stipulates that the data transfer of actual parameter variables to formal parameter variables is "value transfer", that is, one-way transfer , only actual parameters are passed to formal parameters, but not from formal parameters to actual parameters.

The return value of the function:

I hope that the calling function will get a certain value through function call.

(1) The return value of the function is obtained through the return statement in the function.

(2) Type of function value;

(3) If the type of 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 bring back a certain value that the user needs. The function does not bring back a value, but it just does not bring back a useful value and brings back an uncertain value. value.

(5) If you don’t need to bring back any value, use void.

Function call:

Calling method:

1. Function statement;

2. Function expression;

3. Function parameters.

Declaration of the called function:

Conditions for a function to call another function:

1. The function that is called first must It is an existing function;

2. If you use a library function, you should generally use the #include command at the beginning of this file to "include" the information needed when calling the relevant library function into this file. The .h file is the suffix used for header files.

3. If a user-defined function is used, and the function is in the same file as the function using it, the called function should generally be declared in the main calling function.

4. If the called function definition appears before the calling function, it does not need to be declared.

5. If function declarations have been made outside the function before all function definitions, there is no need to declare the functions called in each calling function.

Local variables and global variables:

(1) Local variables Variables defined inside a function are internal variables, which are only valid within the scope of this function. The main function cannot use variables defined in other functions; variables with the same name can be used in different functions. They represent different objects and do not interfere with each other; formal parameters are also local variables; within a function, they can be defined in a compound statement Variables, these variables are only valid in this compound statement. This compound statement can also be called "sub-program" or "program block";

(2) Global variables, variables defined outside the function are called As external variables, global variables can increase the channels for data connection between functions. Generally, they should not be used when it is no longer necessary. They occupy storage units during the entire execution of the program, which is the universality of the function. Using global variables will make the program clearer. reduce. Also note that if an external variable and a local variable have the same name in the same source file, the external variable will be "shielded" within the scope of the local variable and will have no effect.

Storage categories of variables:

(variable value storage time) dynamic storage method, dynamically allocate storage space during program running, static storage method It refers to the way of allocating fixed storage space during the running of the program; the storage space is divided into program area, static storage area and dynamic storage area; all global variables are placed in the static storage area, and the space is allocated when the program starts and released when it is completed; dynamic The following data is stored in the storage area:

1. Function formal parameters;

2. Automatic variables;

3. On-site protection and return address when function is called; in C Every variable and function in the language has two attributes, which are the data type and the data storage type. The storage class is the way the data is stored in memory.

Storage methods are divided into static and dynamic storage categories, including four types: automatic (auto), static (static), register (register), external (extern), if not declared , if you think it is auto type, it will automatically allocate storage space and it is a dynamic storage method.

Static declares that local variables do not disappear after the function call ends but retain 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 you define a local variable without assigning an initial value, the static local variable will automatically be assigned a value of 0 or a null character during compilation. Although the static local variable still exists after the function call is completed, other functions cannot reference it. Static local variables are mainly used when after initialization, the variable is only referenced without changing its value.

Register variable is a C language that allows the value of a local variable to be placed in a register in the CPU and taken out directly from the register to participate in the operation when needed. There is no need to retrieve it from the memory. However, the number of registers in the computer system is limited. , you cannot define any amount of memory arbitrarily, and local static variables cannot be defined as register variables.

Extern declares external variables to expand the scope of external variables. In a file, if the function before definition wants to reference the external variable, the variable should be declared as an external variable using the keyword extern before the reference. In multiple files, you can also use extern declaration to declare external variables. Sometimes you want some local variables to be referenced only by this file and not by other files. In this case, you can add a static when defining external variables, which enhances the versatility in the modular design of the program.

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

Note: These methods also apply to function declarations

The above is the detailed content of Summary of basic knowledge points of C language. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template