How to correctly use the extern keyword in C

后端 未结 10 2263
感情败类
感情败类 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:15

    "extern" changes the linkage. With the keyword, the function / variable is assumed to be available somewhere else and the resolving is deferred to the linker.

    There's a difference between "extern" on functions and on variables: on variables it doesn't instantiate the variable itself, i.e. doesn't allocate any memory. This needs to be done somewhere else. Thus it's important if you want to import the variable from somewhere else. For functions, this only tells the compiler that linkage is extern. As this is the default (you use the keyword "static" to indicate that a function is not bound using extern linkage) you don't need to use it explicitly.

提交回复
热议问题