zoukankan      html  css  js  c++  java
  • C/C++ static vs global

    static has a very simple logic to it. If a variable is static, it means that it is a global variable, but it's scope is limited to where it is defined (i.e. only visible there). For example:

    • Outside a function: global variable but visible only within the file (actually, the compilation unit)
    • Inside a function: global variable but visible only within the function
    • (C++) Inside a class: global variable but visible only to the class

    Now let's see what the C11 standard says regarding static and extern (emphasis mine):

    6.2.2.3

    If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

    6.2.2.4

    For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

    6.2.2.7

    If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

    So the standard says that first, if you have:

    static int m;
    extern int m;

    then the second declaration (with extern) would regard the first one and in the end m would still be static.

    However, in any other case, if there are declarations with both internal and external linkage, the behavior is undefined. This actually leaves us with only one option:

    extern int m;
    static int m;

    i.e., extern declaration before static declaration. gcc was nice enough to give you error in this case of undefined behavior.

  • 相关阅读:
    学习人人都是产品经理03
    Excel 自增
    axios中get/post请求方式
    vue本地跨域实现与原理
    于无声处听惊雷Deepin成长小记
    .NET桌面程序混合开发之三:WebView2与JS的深度应用
    跨平台系统的开发方案整理
    .NET桌面程序混合开发之二:在原生WinFrom程序中使用WebView2
    双系统升级Win11小记.md
    .NET桌面程序混合开发之一:Winform+H5,WebView2概览
  • 原文地址:https://www.cnblogs.com/hustxujinkang/p/4525864.html
Copyright © 2011-2022 走看看