C language-study notes for writing large programs

Reading, you have to nibble, bite the bullet, read slowly, don't rush it!

This is a sentence that I especially want to tell myself and make myself stick to it. Recently, I found that my learning was too impetuous, too demanding for speed and results, but I forgot that learning requires gradual progress and requires steady and steady progress!

The content of this article is recorded while reading the book.

Write large programs

Most large programs are composed of multiple files. Common programs are composed of multiple source files, and there are usually some header files .

The source file contains the definition of the function and global variables (or external variables)

The header file contains information that can be shared between source files

1. Source files

​​​​​​The c program I wrote before is composed of a single file, in fact, we can divide the program into any number of source files. The extension of the source file is .c, and each source file contains part of the program content (mainly the definition of functions and variables). One of the source files must contain the main function, which is the starting point of the program!

Advantages of dividing the program into multiple source files:

1>Putting the functions and variables of the line pipe into the same file can make the structure of the program clearer

2>Each source file can be compiled separately. If the program is large and needs frequent changes (this is a very common phenomenon in the program development process), this method can greatly save time

3> Conducive to the reuse of functions.

2. Header file

When we divide the program into several source files, the question arises: How do functions in a file call functions defined in other files, and how to access external variables in other files? How can two files share the same macro or type definition?

That is to use the #include directive.

The #include directive tells the preprocessor to open the specified file and insert the contents of the file into the current file.

 #include directive

#include directive has two forms

① # include <file name>//This form is only used for the header files of the C language library

② # include "file name"//This form is used for our own source code files

In fact, you can also use the #include path (its portability is relatively poor, of course, you can also use relative paths to solve the portability problem) here is not so detailed

3. Shared macro definitions, shared type definitions, shared function prototypes, shared declared variables

1>Share macro definition, type definition

If multiple source files need to use a macro or a type, you can put these macros and types in a .h file, and then #include ".h" this .h file in the source files that need them.

2>Shared function prototype

What is shared is a statement! (Rather than the definition of the function, the definition of the function is placed in a .c file)

As above, put all the declarations of the functions in the .c source file to be used in a .h file, and then #include ".h" in the main.c file. Usually, the .c file that contains the function is also Need to include the following header file (its function declaration. In order to verify whether the function defined in the .c file matches the function declaration in .h)

3>Shared declared variables (external variables)

Similar to the shared function declaration, but you need to add an extern before the variable declaration, such as extern int i; extern lets the compiler know that a variable i is declared here, it may be defined in another .c file, you need to find it . Note that the declaration does not need to consume memory, while the definition needs to allocate memory space.

4. Header file protection

In C language, if the source file contains a header file twice, a compilation error may occur. (For example, ah includes ch, bh includes ch and main includes "ah" and "bh". At this time, if bh is included twice, a compilation error will occur).

The solution to the above problems is: use macro definitions and conditional compilation.

For example: ch file

#ifndef C_H//如果不存在宏c_H
#define C_H

void max(int a,int b);//某函数的声明

#endif

  This way you can protect the header file.           

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/115340937