A good program first to have a good program structure, I from the variables and structure two aspects to do analysis.
First, talking about the variables in the program
The most basic of a program architecture is the program variables, referring to the variables in the program, we should consider two parts, on the one hand, the scope of the variable, on the one hand, the lifetime of the variable.
We analyze the variables from these two angles:
(1) Global variables
The declaration of a global variable is placed before the main function, it is scoped to the whole program, and the lifetime is the entire period of the program.
(2) Local variables
A local variable is a variable declared in a local scope that is scoped to a specific region, such as a variable scoped within a function body,
The function body execution completes. If a variable is declared inside a for statement, then the area in which he is acting is the for statement. Same life
The cycle is also in a specific area.
(3) Static local variables
The variable scope declared by static local variables is a specific function body, but its life cycle is the life cycle of the whole program.
Attention:
The function body cannot be passed back to the address of the local variable, which is dangerous because the memory area of the local variable is freed after the function body execution is complete.
As an example,
#include <stdio.h>int value = 0;//global variable void fun ();
void Fun1 (); int main () { int i=0; The global variable printf ("main:%d\n", I);//The I used here should be the I value at the main level. Fun (); printf ("main:%d\n", I);//The one printed here is still the I Fun () under Main; Fun1 (); printf ("main:%d\n", I);//The one printed here is still the I fun1 () under Main; printf ("main:%d\n", I);//This print is still i}void fun () { int i = 10;//here the I value only acts on this function body and once each entry function is defined once, leaving the function to free space printf ("fun:%d\n", I);//This local variable is printed here each time is 1}void fun1 () { static int i = 10;//static local variable, which is declared only once, and has survived printf (" Fun1:%d\n ", i); i++;}
Second, the program structure analysis
Large programs typically have to be programmed in a modular format.
Each module corresponds to a header file and a source file
The content of the source file is the subject part of the program, the contents of the function
The contents of the header file are explanations of the program, including declarations of global variables and declarations of all public functions
(1) header file
1. header file contains issues
#include "" and #include < >
"" is the first in the current directory to find this file, if you do not go to the system set up the directory to look for, generally write their own files are used ""
<> is to go directly to the system settings directory to find files, is generally provided by the system file <>
Be aware that anti-duplication containment
So it needs to be written in each header file as follows
(Standard header file structure)
#ifndef _max_h_
#define _max_h_
Program Area
#endif
2, the variables in the header file to be called externally should be used extern
extern int i;
Note: The difference between a declaration and a definition
Define a variable that defines a function to generate code in the program.
and the declaration does not generate code in the program, because the declaration just tells the compiler that I have this thing, let the compiler remember.
For example, before the main function we have to declare the function, and the body of the function is defined after the main function.
(2) Source file
Explanation of the containing function (source code)
Program Structure of C language