extern on function prototypes?

ぃ、小莉子 提交于 2019-12-01 17:31:13

extern is a linkage specifier for global linkage. Its counterpart is static, which specifies file-local linkage. Since global linkage is the default in C, adding extern to the declaration makes no difference for the declaration of a function. For a variable it prevents automatic memory allocation and using it is the only way to just declare a variable at global scope.

If you just google the keyword, you will find many articles e.g. this one: geeks for geeks

I learned the following for variables years ago from an experienced programmer:

glo.h:
#ifndef EXTERN
#define EXTERN extern
#endif
...
EXTERN int gMyVar;
...

main.c:
#define EXTERN
#include "glo.h"

"glo.h" anywhere inlcuded will just declare all global variables. "glo.h" included in main.c will allocate the storage for the variables. I believe this method was common practice.

For a (non-inline) function, it doesn't make any difference, extern is implicit if no storage-class specifier is given (note, that this applies only to functions, objects are different), so it's only a matter of style what you use.

I've seen both (never use extern for functions/only use it for the declarations in the header), maybe some use extern for symmetry with object identifiers, or to make it easier to grep for external symbols.

Choose whatever you prefer and stay consistent, it doesn't make a difference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!