Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is extern keyword for function necessary at all in C?

Tags:

c

extern

It appears to me that even if I refer to a function in another file with out extern declaration, gcc can still compile that unit. So I am wondering whether the extern declaration is necessary anywhere for function? I know that you need extern for variables.

like image 286
Xiaolong Li Avatar asked Mar 10 '11 04:03

Xiaolong Li


1 Answers

functions have extern storage class specifier by default (unless they are explicitly defined as static)

extern Storage Class Specifier

If the declaration describes a function or appears outside a function and describes an object with external linkage, the keyword extern is optional. If you do not specify a storage class specifier, the function is assumed to have external linkage.

....

It is an error to include a declaration for the same function with the storage class specifier static before the declaration with no storage class specifier because of the incompatible declarations. Including the extern storage class specifier on the original declaration is valid and the function has internal linkage.

like image 58
Prasoon Saurav Avatar answered Oct 08 '22 20:10

Prasoon Saurav