Как правило, вы должны определить функции в двух отдельных файлах .c
(скажем, A.c
и B.c
) и поместить их прототипы в соответствующие заголовки (A.h
, B.h
, запомните включают охранников ).
Всякий раз, когда в файле .c
вам нужно использовать функции, определенные в другом .c
, вы будете #include
соответствующий заголовок; тогда вы сможете использовать функции в обычном режиме.
Все файлы .c
и .h
должны быть добавлены в ваш проект; если IDE спросит вас, нужно ли их компилировать, вы должны пометить только .c
для компиляции.
Быстрый пример:
Functions.h
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
/* ^^ these are the include guards */
/* Prototypes for the functions */
/* Sums two ints */
int Sum(int a, int b);
#endif
Functions.c
/* In general it's good to include also the header of the current .c,
to avoid repeating the prototypes */
#include "Functions.h"
int Sum(int a, int b)
{
return a+b;
}
main.C
#include <stdio.h>
/* To use the functions defined in Functions.c I need to #include Functions.h */
#include "Functions.h"
int main(void)
{
int a, b;
printf("Insert two numbers: ");
if(scanf("%d %d", &a, &b)!=2)
{
fputs("Invalid input", stderr);
return 1;
}
printf("%d + %d = %d", a, b, Sum(a, b));
return 0;
}