Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a C function in one file, then call it from another?

If I define a function in file func1.c, and I want to call it from file call.c. How can I accomplish this task?

like image 315
Sam H Avatar asked Nov 24 '10 21:11

Sam H


People also ask

How do you call a function from one C file to another in C?

You must declare int add(int a, int b); (note to the semicolon) in a header file and include the file into both files. Including it into Main. c will tell compiler how the function should be called.

How do you call one function from another function in C?

The main function always acts as a driver function and calls other functions. We can also write function call as a parameter to function. In the below code, first add(num1, num2) is evaluated, let the result of this be r1. The add(r1, num3) is evaluated.


1 Answers

You would put a declaration for the function in the file func1.h, and add #include "func1.h" in call.c. Then you would compile or link func1.c and call.c together (details depend on which C system).

like image 116
David Thornley Avatar answered Sep 22 '22 23:09

David Thornley