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?
5 Answers
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.

- 117,907
- 20
- 175
- 238
-
So by default, functions are already externally visible (externed), but variables are not? – Sep 29 '16 at 08:28
-
-
2Variables (at file scope) are external as well if they do not have `static` qualifier. The rules are a bit more complicated for variables but for functions, it's quite straight forward. You may find [this answer](http://stackoverflow.com/a/32536140/1275169) useful. – P.P Sep 29 '16 at 08:33
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.

- 62,093
- 7
- 131
- 191
-
Probably I need coffee, but compiler will give you _implicit declaration of function_ if you define a function in different module and try to call it from another module. And, also, it assume function return an int. Am I wrong? – LPs Sep 29 '16 at 08:32
-
@LPs that's if no declaration is visible from the call site. But I understood the question as "why don't function declarations need the `extern` keyword like variable declarations do". – Quentin Sep 29 '16 at 08:38
-
1
-
-
1@LPs No modern C compiler will give you an implicit function declaration. That huge language design mistake was fortunately removed from the language in the year 1999, 17 years ago. – Lundin Sep 29 '16 at 08:48
-
@LPs Use `std-c11 -pedantic-errors` and it will give you errors for C language violations. – Lundin Sep 29 '16 at 09:19
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.

- 1
- 3
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

- 33,420
- 29
- 119
- 214
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.

- 64
- 2
- 10
-
You want to include process.h into process. c as well. Doing so the compiler will notify you in case the function's declaration form the .h file and its definition in the .c file were not in sync any more. – alk Sep 29 '16 at 11:49
-