My files are
// main.c
#include "add.c"
int main(void) {
int result = add(5,6);
printf("%d\n", result);
}
and
// add.c
int add(int a, int b) {
return a + b;
}
My files are
// main.c
#include "add.c"
int main(void) {
int result = add(5,6);
printf("%d\n", result);
}
and
// add.c
int add(int a, int b) {
return a + b;
}
There are many great contributions here, but let me add mine non the less.
First thing i noticed is, you did not make any promises in the main file that you were going to create a function known as add(). This count have been done like this in the main file:
before your main function, that way your main function would recognize the add function and try to look for its executable code. So essentially your files should be
Main.c
and // add.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. Including into the second file will allow you to check that declaration is valid (compiler would complain if declaration and implementation were not matched).Then you must compile both *.c files into one project. Details are compiler-dependent.