October 14, 2009

For a simple C program that contains bothe main and one function , can i write the main in a file and the function in a different file then the main will call the function and gives the appropriate result?

Sure you can,you can make 2 .C files, one include .H file and something like:...

Sure you can,
you can make 2 .C files, one include .H file and something like:
#include "secondfile.c" in the first one.
then you can compile them

Like  (0 likes)

An awesome website for advanced programming is http://stackoverflow.com

An awesome website for advanced programming is http://stackoverflow.com

Like  (0 likes)

Just put the sum.c and the sum.h in the same directory than the main.c for...

Just put the sum.c and the sum.h in the same directory than the main.c for now.
Later, if you want to have them in different directories, you'll have to write a makefile or to include sum.h with the right path.

Like  (0 likes)

You need three files...

You need three files

main.c with the main function
sum.h which is included into the main.c file
In this file you declare the function int sum(int,int);
The compiler needs this information to compile main.c
it is a kind of promise that he will later find a file (in our case sum.c) where the
function sum is implemented. Without the header file sum.h
the compiler would complain not to know the function sum().

Like  (0 likes)

[quote name="Thomas Fritz"]Little example:Hi thomas thanks for your answer...

[quote name="Thomas Fritz"]Little example:
Hi thomas thanks for your answer but regarding the { sum.h} , where iam supposed to write it, i wrote the main in a file and the function in a separate file but dont know about the sum.h

Like  (0 likes)

yes that's it.

yes that's it.

Like  (0 likes)

Little example:...

Little example:

main.c:
------------------------
#include"sum.h"
int main()
{
int a = sum(5,5);
}
------------------------

sum.h:
-------------------------
int sum(int,int);
-------------------------

sum.c
-------------------------
int sum(int a,int b)
{
return a+b;
}
-------------------------

g++ main.c sum.c -o sum

Like  (0 likes)

Assuming I understand you correctly, you can include a header and then be...

Assuming I understand you correctly, you can include a header and then be sure to include your implementation .c file in your compilation

Like  (0 likes)