Reputation:
Why we don't use extern when using function from one .c file in another .c file , but we must do extern for variables case? Is it related to linker?
Upvotes: 2
Views: 3886
Reputation: 64
Yes, Let consider you have one .c file as process.c and you declared it in process.h . Now if you want to use the function from process.c to suppose tools.c then simply #include "process.h" in tools.c and use ther function. The process.h and process.c file should be in your project.
process.c file
#include<stdio.h>
#include<conio.h>
#include "process.h"
unsigned int function_addition(unsigned int a, unsigned int b)
{
unsigned int c = 0;
c = a + b;
return c;
}
process.h:
<bla bla bla >
unsigned int function_addition(unsigned int a, unsigned int b);
<bla bla bla >
tools.c file:
#include<stdio.h>
#include<conio.h>
#include "process.h"
my_tools()
{
unsigned int X = 1, Y = 9, C = 0;
C = function_addition(X,Y);
}
All these files are in one project.
Upvotes: 0
Reputation: 34678
Demo program,
one.c
#include "one.h"
void func1() //defination
{
//code
}
one.h
void func1(); //declaration
main.c
#include <stdio.h>
#include "one.h"
int main()
{
func1();
}
Then compile program in Gcc Linux : gcc main.c one.c
Upvotes: 0
Reputation: 1
you can create a .h
file,declare functions you want to use in the other .c
files and #include
the .h
file in the other .c
files.
Upvotes: 0
Reputation: 63154
Actually, function names act just like variable names, but function prototypes are extern
by default.
From cpprerefence:
If a function declaration appears outside of any function, the identifier it introduces has file scope and external linkage, unless static is used or an earlier static declaration is visible.
Upvotes: 1
Reputation: 121427
Functions are extern qualified by default (unless you change it to internal with static
). For example,
int func(void) {
}
extern int func2(void) {
}
Both func
and func2
are external. The extern
keyword is optional for external functions.
Upvotes: 3