Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke function from external .c file in C?

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; } 
like image 395
ProtoTyPus Avatar asked Jan 21 '14 14:01

ProtoTyPus


People also ask

Can you include a .C file in C?

You can properly include . C or . CPP files into other source files.

Can you include a .C file in header?

The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.


2 Answers

Use double quotes #include "ClasseAusiliaria.c" [Don't use angle brackets (< >) ]

And I prefer to save the file with .h extension In the same directory/folder.

TLDR: Replace #include <ClasseAusiliaria.c> with #include "ClasseAusiliaria.c"

like image 106
Gaurav Jain Avatar answered Sep 23 '22 14:09

Gaurav Jain


Change your Main.c like so

#include <stdlib.h> #include <stdio.h> #include "ClasseAusiliaria.h"  int main(void) {   int risultato;   risultato = addizione(5,6);   printf("%d\n",risultato); } 

Create ClasseAusiliaria.h like so

extern int addizione(int a, int b); 

I then compiled and ran your code, I got an output of

11 
like image 28
Elliott Frisch Avatar answered Sep 20 '22 14:09

Elliott Frisch