Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call another C program?

Status
Not open for further replies.

rpk2006

Technical User
Apr 24, 2002
225
IN
Hi,

I have one MAIN C program in which I have to call other C program
files. How to call them?
Suppose the main program is MAIN.C and I have to call a function
called "Add" located in another C program called "Second.C".
How to call?

Secondly, calling another C program is good, or should I copy the
MAIN function of "Second.C" and make an independent function of
my main program.


---------------------------------
Securing a computer system has traditionally been a battle of wits: the penetrator tries to find the holes, and the designer tries to close them. � M.Gosser
 
What are you asking? How to execute another program from a C program, or how to call a function from another source file?

I'm guessing you mean the latter. Make sure you know the difference between program and function; they're not interchangable.

Compile main.c and second.c separately, then link them together.

I can't understand your last paragraph, but I think the answer you want is: in general, it's definitely better to compile code separately and have a modular application than to have everything in one file and have a monolithic one. If everything's in one file and you make a change, you have to recompile the entire thing. If code is spread out among different files, you only have to recompile the part you changed.
 
Try using header files. Put the function declaration of add in second.h, for example

Code:
//second.h

int add(int, int);

Then in second.c have the function definition

Code:
//second.c
#include "second.h"

int add(int a, int b) {
.....
}

And in main.c

Code:
//main.c
# include "second.h"

void main(void) {
  in a, b, c;
  ....

  b = add(a, c);
  ....
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top