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.

C/C++ static vs global的更多相关文章

  1. static 和 global

    global global关键字如果用在function内部,则说明这个function内用的这个变量是全局的,全局变量就是在整个页面里都能起作用.例如 $conf = 1; function con ...

  2. swoole为什么不建议使用static和global

    $http = new swoole_http_server("0.0.0.0", 9501); $http->on("request", functio ...

  3. error X3025:global variables are implicitly constant, enable compatibility mode to allow modification

    global variables are implicitly constant, enable compatibility mode to allow modification http://xbo ...

  4. php中变量引用&不可与global共同使用

    问题来源,新公司debug. 程序中代码大致如下 class Ci { private static $instance = NULL; public $name = 'hello'; public ...

  5. 从DOM操作看Vue&React的前端组件化,顺带补齐React的demo

    前言 接上文:谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo 上次写完博客后,有朋友反应第一内容有点深,看着迷迷糊糊:第二是感觉没什么使用场景,太过业务化,还不如直接写Vue ...

  6. [c++] Smart Pointers

    内存管理方面的知识 基础实例: #include <iostream> #include <stack> #include <memory> using names ...

  7. [Scala] Scala基础知识

    Object An object is a type of class that can have no more than one instance, known in object-oriente ...

  8. PHP变量作用域详解(二)

    学过C的人用PHP的时候一般会相当顺手,而且感到PHP太方便太轻松.但在变量作用域这方面却与C有不同的地方,搞不好会相当郁闷,就找不到错误所在.昨晚就与到这么一个问题,是全局变量在函数中的问题.今天搜 ...

  9. effective OC2.0 52阅读笔记(六 块与大中枢派发)

    派发队列:dispatch_queue 操作队列:NSOperationQueue  组:dispathc_group_t 37 理解“块”这一概念 总结:块就是一个值,且自有其相关类型.块的强大之处 ...

随机推荐

  1. 修复iPhone的safari浏览器上submit按钮圆角bug

    今天在公司写了一个登录页面效果,让我碰到一个怪异的问题——"表单中的input type=submit和input type=reset按钮在iPhone的safari浏览器下圆角有一个bu ...

  2. kmeans算法原理以及实践操作(多种k值确定以及如何选取初始点方法)

    kmeans一般在数据分析前期使用,选取适当的k,将数据聚类后,然后研究不同聚类下数据的特点. 算法原理: (1) 随机选取k个中心点: (2) 在第j次迭代中,对于每个样本点,选取最近的中心点,归为 ...

  3. 笔记2:傻瓜式盗QQ程序

    1.一个简单的盗QQ号的方法 仿做一个QQ的窗体 PS:当然里面有用的控件只有两个输入框和一个登陆按钮,其他的尽量做得像一些吧! 点登陆时的后台代码: PS:需要导入两个包:using System. ...

  4. iisreset和w3wp的关系

    iisreset是iis自带一个命令行工具.用法: iisreset [computername] /RESTART          停止然后重新启动所有 Internet 服务. /START   ...

  5. jdbc 配置properties实现

    package com.web.study; import java.io.InputStream; import java.sql.Connection; import java.sql.Drive ...

  6. C#与Java中相等关系

    最近在学习java的语法,发现java中两个字符串某些情况下不能用==这个来判断,比如自己定义的类中有String属性,那么就不能用==判断两个是否相等.这才开始比较C#和Java在这里的区别,最后发 ...

  7. Play framework 2.0 -应用程序全局设置(转)

    转载自: http://shenbai.iteye.com/blog/1517366 1.全局对象 在工程中定义全局对象可以允许你操作你的应用程序的全局设置.这个全局对象必须定义在根包下. impor ...

  8. BZOJ4006 [JLOI2015]管道连接

    裸的状压DP 令$f_S$表示包含颜色集合S的最小斯坦纳生成森林的值,于是有: $$f_S=\min\{f_S,f_s+f_{S-s}|s\subset S\}$$ 然后嘛...还是裸的斯坦纳树搞搞. ...

  9. php定时执行PHP脚本一些方法总结

    本文章总结了php定时执行PHP脚本一些方法总结,有,linux中,windows,php本身的方法,有需要的朋友可参考参考. linux下定时执行php脚本 执行PHP脚本 方法1如果你想定时执行某 ...

  10. centos 卸载软件·

    centos下完全卸载php 1显示相关软件的列表 rpm -qa  | grep i(可以不加) php 2 卸载即可 rpm -e 软件名 --nodeps centos下完全卸载mysql 1显 ...