Static vs global

前端 未结 5 508
不知归路
不知归路 2020-11-28 19:57

If I have a C file like below, what is the difference between i and j?

#include 
#include 

static i         


        
5条回答
  •  猫巷女王i
    2020-11-28 20:19

    Scope of static variable/function is within the same file despite you include the file as part of a different source file.

    Scope of global variable is throughout the files in which it is included. To include the variable in a different source file, we use extern before the variable declaration. No memory is allocated again for the variable in this case.

    extern is used to declare a C variable without defining it. extern keyword extends the visibility of the C variables and C functions. Since functions are visible through out the program by default, the use of extern is not needed in function declaration/definition. Its use is redundant.

提交回复
热议问题