Question:
how can i call functions from anther file to my C file.?
anonymous
2011-09-16 05:02:16 UTC
hi,

how can i call functions from anther file to my C file.

for example,

I am write main function in mymain.c and want to call fun1() from callfun.c in another file.

now I want to call callfun.c from my main() function in mymain.c.

Can i include the c file callfun.c or how can i call the function fun1().

thank you.
Four answers:
peter
2011-09-16 05:15:32 UTC
i only know how to do that in c++ by using class

create header class and put your function fun1() in your class body. remember to include the name of your class in your main.cpp (ex, #include "myClass.h")

create and object class in your main.cpp and call the function



For more detailed info, you can get it from deitel book.
anonymous
2011-09-16 12:05:04 UTC
I'm not sure how u should do this in C. but normally U'll need to make a reference to the other file / namespace. And then create a new instance of the class in this namespace or file.



Hope this will help a little bit
Ashish Saran
2011-09-16 12:07:45 UTC
Just include your callfun.c file to your main program..

# include "callfun.c"



now you can call the fun1();
jplatt39
2011-09-16 12:48:55 UTC
Here is a file which is c++ but calls functions the exact same way a c file would:

#include

#include



using namespace std;



extern double fahrtocels(double convert);



extern double celstofahr(double convert);



int main()

{

int choice;

double temperature;



cout << "Enter a temperature to convert:";

cin >> temperature;



cout << "Press 1 to convert from Fahrenheit to Celsius." << endl;

cout << "Press 2 to convert from Celsius to Fahrenheit." << endl;

cin >> choice;



switch (choice) {



case 1: cout << fahrtocels(temperature) << "C" << endl;

break;



case 2: cout << celstofahr(temperature) << "F" << endl;

break;



default: cout << "Please enter 1 or 2.";

}



return 0;

}



The functions fahrtocels() and celstofahr() are in a file called convert.cc. And this is my makefile:

SRCS=main.cc convert.cc



convert: main.o convert.o

g++ -o convert -g main.o convert.o



main.o: main.cc

g++ -g -c main.cc



convert.o: convert.cc

g++ -g -c convert.cc



In other words if I were doing it from the command line and it was c my command would be:



gcc -o convert main.c convert.c



Look up the extern key word.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...