Nov 20, 2017

[C/Cpp] How to include an external function in a C code

If you'd like to use an external function of add.c in main.c, you need to import the function in main.c.
Then, compile main.c with having add.c in same folder.

#include <stdio.h>
#include "add.c"
extern int add(int x, int y);
extern int sum;
void main(void)
 int a=2, b=5;
 sum = add(a,b);
 printf("%d\n", sum);

#include <stdio.h>
int add(int x, int y);
int sum=0;
int add(int x, int y)
 sum = x+y;
 return sum;

No comments:

Post a Comment