Is extern keyword for function necessary at all in C?

前端 未结 4 1222
滥情空心
滥情空心 2020-12-05 11:25

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 declar

4条回答
  •  不思量自难忘°
    2020-12-05 11:52

    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).

提交回复
热议问题