
Source from the Internet, please delete if infringing
C language is a programming language used for computer programming. It has characteristics of both high-level languages and assembly languages. It can be used as a system design language to write system application programs, or as an application design language to write applications that do not depend on computer hardware.
In fact, C language is a small core language that includes very few hardware-related parts. C language does not directly provide input and output statements; file operations, dynamic memory management, and other operations require the use of library functions provided by the system. It can be said that C language is a structured programming language, which is a divided idea. For example, in a Hello World program, two functions can be created, one for outputting Hello and one for outputting World.
2. List of Data Types in C Language (to be explained later):
Integer, Floating Point (real number), Character, String, Array, Pointer, Structure, Union
3. List of Control Statements in C Language (to be explained later):
Jump statements: goto statement, break statement, continue statement, return statement
Conditional statements: if-else statement
Loop statements: do-while statement, while statement, for statement
Selection statement: switch-case statement
4. The simplest C program:
#include<stdio.h> int a=3; // This is a global variable a can be used throughout the programvoid f(int n); // here n is the formal parameterint main(void){ int n; // define n here, which does not affect the formal parameter n int b=50; // this is a local variable b becomes invalid outside the main function int a=2; // define a here, which is not related to the outer a if(a>b){ // this is a compound statement (including other loops, selection statements) int a=1; // define a here, which is not related to the outer a but // according to the characteristics of variables, when the same variable name is used // the variable in the compound statement will override the outer main function's a, and the main function's a will override the outer // global variable a // (small overrides large)}}void f(int n){ int n;// define n here, which is also unrelated to the formal parameter n}
1. A program consists of one or more source files, and a source file contains only one main function.
2. Preprocessing directives:
1. For example, #include<stdio.h>, (there are other directives, which will be discussed later) the C compilation environment will first perform a task before “translating” the source program, which is to process the preprocessing directives by a preprocessor. For the <stdio.h> directive, when using input and output functions from the standard library, the compilation system requires the program to provide relevant information. #include is called the file inclusion command, and its role is to provide this information by introducing the corresponding header file (.h file), so that library functions can be called.
2. The processing of #include is simple; it inserts the contents of the header file at the position where the command is located, thus connecting the header file and the current source file into one source file, which is the same as the effect of copying and pasting.
3. The difference between using angle brackets < > and double quotes ” ” lies in the search paths for header files:
When using angle brackets < >, the compiler will search for header files in the system path;
When using double quotes ” “, the compiler will first search for header files in the current directory. If not found, it will then search in the system path.
5. Global Variables and Local Variables:
#include<stdio.h>int a=3;// This is a global variable a can be used throughout the programvoid f(int n);// here n is the formal parameterint main(void){int n;// define n here, which does not affect the formal parameter nint b=50;// this is a local variable b becomes invalid outside the main functioninta=2;// define a here, which is not related to the outer aif(a>b){// this is a compound statement (including other loops, selection statements)inta=1;// define a here, which is not related to the outer a but// according to the characteristics of variables, when the same variable name is used// the variable in the compound statement will override the outer main function’s a, and the main function’s a will override the outer// global variable a // (small overrides large)}}void f(int n){int n;// define n here, which is also unrelated to the formal parameter n}Copy code
1. A local variable can only be used within the function where it is declared; once outside the function, it cannot be used. Different functions can use the same variable name, representing different objects, and do not affect each other. Formal parameters are also local variables, and variables can also be defined within compound statements in a function, which are only valid within that compound statement. Compound statements are referred to as subprograms or program blocks.
2. A global variable can be used not only in the main function but also in other functions, meaning that global variables can be used anywhere in the program. They can increase the connection of data between functions, and during the entire execution of the program, they occupy storage units, which gives them universality. In summary, the scope of the two is different.
6. Definition of C Functions:
A C program consists of one or more functions, but there must be one and only one main function, and the program starts from the main function.
A function consists of two parts: 1) Function header, the first line of the function: function type, function name, (function parameter type, function parameter name), which is the (formal parameter), which can be void or nothing. 2) Function body, which represents the program under the function’s curly braces, generally includes: declaration part, usually a variable must be declared before it is used, and each line of data declaration or C statement must have a semicolon at the end to indicate termination.
7. Explanation:
The C language itself does not provide input and output statements; input and output operations must call system library functions such as scanf and printf, which are in the <stdio.h> file, so this header file must be preprocessed at the beginning.
As for formal parameters and actual parameters: when we call a function, the parameters passed to it and the parameters received within the function body are not the same variable. The parameters passed to the function when calling it are called actual parameters (or实参), while the parameters used to receive the externally passed parameters in the function body are called formal parameters (or形参).
8. Steps for Running a C Program:
Programming—>Compiling—>Linking
1. Programming: Write the program in the compilation environment, and once completed, the system will store the source program as a file with a .c suffix in a specified folder, such as f.c.
2. Compiling: The compilation environment will perform syntax checks on the source program, issuing error messages to remind programmers to modify until there are no syntax errors. The compilation program automatically converts the source program into a binary object file, saved with an .obj suffix.
3. Linking: The binary object files obtained after compilation cannot be executed directly by the computer; they are still just scattered parts and must be combined by a linking program with the compiled object files, function libraries, and other generated files to create an executable file that the computer can run. The suffix is .exe.
9. Storage Methods and Lifetimes of C Variables:
1. In C language, the scope of variables can be divided into global variables and local variables, while from another perspective, variable storage can be divided into two different methods: static storage and dynamic storage. Static means that a fixed space is allocated by the system to the program during its execution, while dynamic means that during execution, storage space needs to be dynamically allocated according to the situation.
2. The usage of storage space in memory can be divided into three types:
(1) Program area
(2) Static storage area, where global variables are stored, responsible for allocating memory during program execution. In the static data area, all bytes in memory are initialized to a default value of 0x00.
(3) Dynamic storage area, which stores the following data:
1. Function formal parameters, allocating storage space for formal parameters when calling functions.
2. Automatic variables in functions.
3. On-site protection and return addresses during function calls, etc.
4. There are four types of variable storage: auto (automatic variable), static (static variable), register (register variable), extern (external variable).
For all these data, dynamic storage space is allocated when the function starts and released when the function ends. In C language, each variable and function has two attributes: data type and storage category.
1- auto (automatic variable)
In the local variables of a program’s function, if not specifically declared as static (static), they are automatically allocated dynamic address storage. This includes compound statements and function formal parameters. Automatic variables are declared with the keyword auto. The key point is that these variables will be released back to memory by the system after the function call is completed.
2- static variable (local variable)
Sometimes, we want our local variables not to disappear after the function call ends but to retain their original values. Their occupied storage units are not released, declared with the keyword static. These variables are called static variables. They are similar to global variables in that, after the function call ends, they do not release memory and retain their current values throughout the program execution. However, they are essentially local variables and cannot be used in other functions. Moreover, they can only be assigned a value the first time; throughout the program execution, they will only retain the value from the last function call.
A static global variable can be accessed by functions within the module (source program) but cannot be accessed by functions outside the module. It is a local global variable.
A static function can only be called by other functions in this source program, meaning that this function is limited to the local scope of the module that declares it.
When a global variable is prefixed with static, it becomes a static global variable. When a source program consists of multiple source files, non-static global variables are effective in each source file, while static global variables limit their scope, meaning they are only effective within the source file defining the variable. Since the scope of static global variables is limited to one source file, they can only be shared among functions within that source file, thus avoiding errors in other source files.
Changing a local variable to a static variable alters its storage method and lifespan. Changing a global variable to a static variable changes its scope, limiting its usage range (only locally).
Static functions have a different scope from ordinary functions and can only be used within this source file.
3- register (register variable)
If a variable is frequently used during program execution, the system must access memory units multiple times, which affects program execution efficiency. Thus, to improve efficiency, register variables are introduced, which can store the values of local variables directly in the CPU’s registers. When needed, the parameters can be directly fetched from the registers for computation without going through memory for storage. Due to the limitations of hardware register lengths, register variables can only be char, int, or pointer types. The register specifier can only be used for variables and formal parameters within functions; thus, external variables or static variables cannot be declared as “register”. Since the access speed of registers is much higher than that of memory, this can improve execution efficiency.
4- extern (external variable)
Generally, external variables are global variables defined outside of functions.
If there is no definition of external variables in the header file (knowledge of files),
then if a program needs to extend the scope of a global variable from one source file to another, the extern keyword can be used in the function definition to turn that variable into an “external variable”, indicating that the scope of this variable is extended to this position. With this declaration, this variable can be used in other source files.
To extend the scope of a global variable to other files, one can first define a variable in one file and then use extern in another file with the same variable name, which extends the scope to the end of the entire program.
*Note: If a global variable in file 1 is prefixed with static, then this variable will be limited to that file, and extern in file 2 will not be able to access the global variable from file 1.
Adding extern before a function declaration allows that function to have an extended scope and be used in other files.
if you are over 18 years old and find learning 【C language】 too difficult? Want to try other programming languages? I recommend you learn Python, currently, there is a value of 499 yuan Python zero-based course available for free, limited to 10 spots!
