Reference the custom C function module in the C program.
We know that at the beginning, c language programming is usually used to write code in A. c or. cpp (only. c) file. There must be an entry function,
That is, the main () function. You can write all the program code in the main function. If you want the program to be more modular, you can also write some operations in a function.
The declarations and definitions of these functions are also in the main function.
Think about it. As your code grows, more and more functions are implemented. In a. c file, you define many functions that implement different functions,
And they are all mixed up. Do you feel confused when you look at the code you write? Here I found a way to make the program more modular,
More organized. The procedure is as follows:
1. Write the declaration of user-defined functions with similar functions in a. h file (for example, Math. h)
2. Write the specific implementation of these functions in the. c file (for example, Math. c should include the header file # include "Math. h ")
3. include the header file (# include "Math. h") in your main program (Supporting. c file formats), and then you can call these udfs in the main program.
Now we want the C function module, which provides mathematical computing functions (such as addition, subtraction, multiplication, division, and other computing functions). The user-defined functions are written in Math. h.
The implementation is written in Math. c and the main function main. c calls these functions for testing.
I. Compile Math. h file
1 # ifndef _ MATH_H 2 # define _ MATH_H 3 4 # include <stdio. h> declaration of udfs 5 6/* 7 8 */9 // Add 10 int Add (int a, in B ); 11 // minus 12 int Sub (int a, int B); 13 // multiply by 14 int Multi (int a, int B ); 15 // except 16 double Dev (int a, int B); 17 //... 18 # endif
2. Compile the Math. c file
1 # include <stdio. h> 2 # include "Math. h" // must be added !!! 3 4 int Add (int a, int B) 5 {6 return a + B; 7} 8 9 int Sub (int a, int B) 10 {11 return a-B; 12} 13 14 int Multi (int a, int B) 15 {16 return a * B; 17} 18 19 double Dev (int a, int B) 20 {21 if (B = 0) 22 {23 printf ("\ n division cannot be 0. "); 24 return 0.0; 25} 26 return (double) a/B; 27}
Iii. Test: Functions in the calling module of main. c (Supporting. c file format)
1 # include <stdio. h> 2 # include <conio. h> 3 # include "Math. h "// Add the header file of the UDF 4 5 void main () 6 {7 int a = 15, B = 10; 8 9 // call the custom function 10 printf ("a + B = % d \ n", Add (a, B )); 11 printf ("a-B = % d \ n", Sub (a, B); 12 printf ("a * B = % d \ n", Multi (, b); 13 printf ("a/B = % f \ n", Dev (a, B); 14 15 getch (); 16 return; 17}
Note:When you add the main program code in VC6.0, you must add the. c format (main. c). if you add the. cpp format (main. cpp), an error is returned,
I still don't know the specific reason. If you want to know it, please let me know!
Running result:
Appendix: The above content is a summary of my learning from the book C Language Science and Art for everyone to learn and share.