How to correctly use the extern keyword in C

后端 未结 10 2343
感情败类
感情败类 2020-11-22 08:11

My question is about when a function should be referenced with the extern keyword in C.

I am failing to see when this should be used in practice. As I

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 08:16

    Functions actually defined in other source files should only be declared in headers. In this case, you should use extern when declaring the prototype in a header.

    Most of the time, your functions will be one of the following (more like a best practice):

    • static (normal functions that aren't visible outside that .c file)
    • static inline (inlines from .c or .h files)
    • extern (declaration in headers of the next kind (see below))
    • [no keyword whatsoever] (normal functions meant to be accessed using extern declarations)

提交回复
热议问题