Modular programming:
We can all see that the programs written by others are neat, standardized, and readable. Not all programs and functions unload a main file, but the entire project is subdivided clearly below, high portability.
Modularize a program or function with independent overall functions into another file, and manage it in a unified manner. Finally, the program is called in an orderly manner under # include "xxxx. h" in the main file.
The following describes how to modularize the program:
1. Create a head file named "people. h" and add the following content:
# Ifndef _ lele__h _ // pre-compiled header file ifndef => if no define, if the project does not define this all // file, use the _ PEOPLE__H _ constant to judge
# Define _ lele__h _ // If the header file "people. h" is not defined, it can be defined. If the header file is already defined, the header file will not be compiled // translated.
// Function declaration
Char * run ();
Void say (char * str );
Void eat (char str []);
Int calculate (int a, int B );
# Endif // end character
2. Create a people. c file. Let's use the people. h function declaration in the file and add the following content:
# Include "people. h"
# Include "app. h"
# Include <string. h>
// Function implementation
Char * run ()
{
Discance = 100; // change the value of the extern variable, that is, change the value of the extern variable.
Char * ch = "he has runned about ";
Return ch;
}
Void say (char * str)
{
Printf ("I said ");
While (* str! = NULL)
{
Printf ("% c", * (str ++ ));
}
Printf ("\ n ");
}
Void eat (char str [])
{
Char * p = str;
Printf ("I am eating ");
While (* p! = NULL)
{
Printf ("% c", * (p ++ ));
}
Printf ("\ n ");
}
Int calculate (int a, int B)
{
Int sum;
Sum = a * B;
Return sum;
}
3. This completes the ing between a pair of C (source file) and H (header file.
Next, let's take a look at the use of extern. We create a header file app. h and add the following content:
# Include <stdio. h>
Extern int discance; // It can only be declared here and cannot be initialized. Otherwise, it will be wrong.
4. Finally, we will write the most familiar main file:
# Include "app. h"
# Include "people. h"
// Re-define and initialize to prove that this external variable is under the current data file,
// If app. h is introduced externally, sum can be used to change the value of sum,
// It is equivalent to changing the value in the current file.
Int discance = 0;
Void main ()
{
Char * p;
Printf ("Welcome to modular programming \ n ");
Eat ("meat ");
Say ("hello world ");
P = run ();
While (* p! = NULL)
{
Printf ("% c", * (p ++ ));
}
Printf ("% d meter! \ N ", discance );
}
In this way, simple Modular programming is completed, and it doesn't matter if you don't understand it. You can download the source file and run it on VC. There are C and C ++ versions, you can make a comparison.
This article is from the "I am a driver-MyWay" blog, please be sure to keep this source http://dragonyeah.blog.51cto.com/7022634/1285921