[c] 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;
}

This question is related to c function external

The answer is


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

make a file classAusiliaria.h and in there provide your method signatures.

Now instead of including the .c file include this .h file.


You can include the .c files, no problem with it logically, but according to the standard to hide the implementation of the function but to provide the binaries, headers and source files techniques are used, where the headers are used to define the function signatures where as the source files have the implementation. When you sell your project to outside you just ship the headers and binaries(libs and dlls) so that you hide the main logic behind your function implementation.

Here the problem is you have to use "" instead of <> as you are including a file which is located inside the same directory to the file where the inclusion happens. It is common to both .c and .h files


you shouldn't include c-files in other c-files. Instead create a header file where the function is declared that you want to call. Like so: file ClasseAusiliaria.h:

int addizione(int a, int b); // this tells the compiler that there is a function defined and the linker will sort the right adress to call out.

In your Main.c file you can then include the newly created header file:

#include <stdlib.h>
#include <stdio.h>
#include <ClasseAusiliaria.h>

int main(void)
{
    int risultato;
    risultato = addizione(5,6);
    printf("%d\n",risultato);
}

 write main.c like this - 
 caution : while linking both main.0 and ClasseAusiliaria.o should be 
 available to linker.

 #include <stdlib.h>
 #include <stdio.h>
 extern int addizione(int a, int b)

 int main(void)
 {
     int risultato;
     risultato = addizione(5,6);
     printf("%d\n",risultato);
 }

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:

    int add(int a, int b); 

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

    int add(int a, int b);

    int main(void) {
        int result = add(5,6);
        printf("%d\n", result);
    }  

and // add.c

    int add(int a, int b) {
        return a + b;
    }

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.


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to external

How to invoke function from external .c file in C? How to style SVG with external CSS? How to call execl() in C with the proper arguments? Android saving file to external storage Find location of a removable SD card PHP ini file_get_contents external url Getting the 'external' IP address in Java Eclipse: How to build an executable jar with external jar?