What is the use of Static local variable when we can get a global variable at the same cost?

前端 未结 6 645
迷失自我
迷失自我 2020-12-05 08:54

In C ,what is the use of static storage class when an external variable can serve its purpose at the same cost ie. both occupy storage space in the data segment of the execu

6条回答
  •  心在旅途
    2020-12-05 09:22

    Difference between local and global first and foremost is the scope: you can access local variables only from within the block they're defined in, while global variables can be accessed from anywhere. Consequently, you can only have one variable with a given name in global scope, but you can have multiple local static variables in different functions.

    As with static global variables versus extern variables: yes, static global variables are local to the translation unit (i.e. the .c source file they're defined in).

    So the main concern here is the notion of scope, and the storage comes naturally from there.

提交回复
热议问题