static declaration of m follows non-static declaration

前端 未结 4 1159
暖寄归人
暖寄归人 2021-01-02 06:16

I am trying a small example to know about the static external variable and its uses. The static variable is of local scope and the external variable is of global scope.

4条回答
  •  醉梦人生
    2021-01-02 07:14

    Remember this (quoting Eli Bendersky):

    • A static variable inside a function keeps its value between invocations.
    • A static global variable or a function is "seen" only in the file it's declared in

    In your code, static int m = 25; means that m's scope is limited only to that file, that is, it is only visible inside static5.c and nowhere else.

    If you would like to make use of m outside of static5.c make sure to remove the keyword static from the declaration of the variable.

    For a more canonical explanation, along with an example, see this answer by Eli Bendersky

    EDIT: (according to Klas' recommendation) **The actual scope is a compilation unit, not the source file. The compilation unit is the way the file looks after the preprocessor step

提交回复
热议问题