rinn said:
I have written a main() program in sap.c and created one external
function in another file fun.c my doubt is how to link these two files
sap.c, fun.c and run a program inorder to get a result.i'll be happy if
anyone gives the procedure.
/*
sap.c
*/
#include <stdio.h>
#include "fun.h"
int main(void)
{
int x;
x = afunction("how many xes are in this string?");
printf("function returned %d\n", x);
return 0;
}
/*
fun.h
*/
#ifndef fun_h
#define fun_h
int afunction(const char *str);
#endif
/*
fun.c
*/
static int countchars(const char *str);
int afunction(const char *str)
{
return countchars(str, 'x');
}
static int countchars(const char *str, char ch)
{
int answer = 0;
while(*str)
{
if(*str == ch)
answer++;
str++;
}
return answer;
}
That shows tha basic program layout.
Put sap.c, fun.c and fun.h into the same directory (sometimes you can make
the complier use fancier structures, do i the easy way for now).
If you are using a command line complier, generally you need to type the
names of all the .c source files. It will then compile them all and link.
if you are using an IDE like Visual C++, you need to create a "project" file
in some internal format, which contains the names of all your source files,
the build from the visual tools.
When your project gets really complicated you need to move to makefiles, but
that's a whole differnet story.