Using the extern keyword
4 answers
They are optional for function declarations. They are only needed to declare external global variables:
// header
extern int foo;
// implementation (.c)
int foo;
Wihout extern
, the compiler will create a global variable every time it encounters it (because the header is included) and you get a linker error.
Another use case for this keyword is to make C ++ code compliant by specifying it with the C binding (this again prevents linker errors, namely those caused by C ++ name change):
#ifdef __cplusplus
extern "C" {
#endif
void foo(void);
#ifdef __cplusplus
}
#endif
+1
user529758
source
to share