C language Learning _c How to invoke a function from another source file in a file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.