Alex Rivera | Logout

Is extern keyword for function necessary at all in C?

Asked 2011-03-10T04:39:22.983
18

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.

Edit
Report

1 Answer

12

It's not necessary, but I prefer it in headers to reinforce the idea that this function is defined somewhere else.

To me, this:

int func(int i);

is a forward declaration of a function that will be needed later, while this:

extern int func(int i);

is a declaration of a function that will be used here, but defined elsewhere.

The two lines are functionally identical, but I use the extern keyword to document the difference, and for consistency with regular variables (where the difference is important, and has exactly that meaning).

answered 2011-03-10T04:46:09.817

Your Answer