Internal linkage with static keyword in C

元气小坏坏 提交于 2019-11-27 22:19:59

If you have a global variable declared in a .c file, what is the difference between using static and not using static? Either way, no other .c file has access to the variable [...]

A different file could declare x:

extern int x;

That would allow code referencing x to compile, and the linker would then happily link those references to any x it finds.

static prevents this by preventing x from being visible outside of its translation unit.

There is only one "namespace", so to speak, in C. Without the "static" keyword you are not protected from another file using the name "x" (even if you do not make it visible in your own library's header).

Try to link together several C files containing a non-static variable x (interleaving read and write accesses from functions in each file), and compare with the situation where these variables are declared static.

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