How to call another source file function in C language

Source: Internet
Author: User
Calling a function in a2.c in the source file a1.c
There are two ways of doing this:
1. There is a complete function definition in a2.c, add the function prototype (declaration) to be used in a1.c, for example:
In a2.c: There is a function void A2 () {...};
In a1.c: Add a line to the front of the file: void A2 ();
  
2. Write the prototype of the fully defined function in a2.c to a A2.h file, and add # include "A2.h" to the a1.c file header.
Both of these methods can be called normally in a1.c, just as in a2.c.

In fact, the function is external by default, as long as it is declared in other files, but note that if it is preceded by static, it can only be used in this file and can no longer be called by other files.


How to write a header file
1. An. h file should have a. c file, which helps to view and modify the program. such as A.h and A.C;:
In the. h file
#ifndef __a_h_
#define __a_h_

void Trans2 (double B);
Double trans1 () ;
#endif

2. Write the corresponding function definition in the. c file

3. Add # include "A.h" at the beginning of the Main.c file

How to call another source file function in C language

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.