Problem
c How to invoke a function from another source file in one file, title.
Solutions
When the program is a lot more code, want to modular development, save a bit in different files, is a good solution, then how can we make the code in each file work together? We know that the main function is the entry of the program, we want to write different functions in different functions, and put these functions uniformly into another file, so that the main function is too long, the main function can be used in a method when the call to deal with. In order to implement this step, we do so. First, define a C code header file, such as Function.h, in which to declare the function to be implemented, such as int add (int a,int b); , and then create a new source file for FUNCTION.C, at the beginning of FUNCTION.C # include "Function.h", and then write down the implementation of the declared function in the header file. After this is finished, the main function if you want to invoke the function in the source file, only need to add #include<function.h> at the beginning of the main function, and so on, the main function calls the corresponding function will automatically find the implementation of the program part of the code.
Code implementation
1 # include<stdio.h>23int Add (int A,int b);
Function.h
1 #include <function.h>23int Add (int A,int b )4{5 return +B; 6 }
function.c
1# include<stdio.h>2# include<function.h>3 4 intMain ()5 {6 intA =1, B =2;7 intc = Add (A, b);//here is a call to the Add function in Function.c8printf"c=%d", c);9 Ten return 0; One}
main.c
This is the implementation of the main function called other files in the function, can be easily modularized development.
C language Learning _c How to invoke a function from another source file in a file