海湾合作委员会是否自动初始化静态变量为零? [英] Does gcc automatically initialize static variables to zero?

查看:93
本文介绍了海湾合作委员会是否自动初始化静态变量为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态变量声明,但在函数初始化。将这个变量初始化自动为零?

 静态INT IDX;


解决方案

是 - C标准 ISO / IEC 9899:1999 又名 C99 (和C ++)的标准说,这一定是这样。详见第6.7.8(初始化) WG14 N1256 项目10对于确切的文本。

正如其他人所指出的那样,这是很好的做法,总是初始化静态变量:

 静态INT IDX = 0;

这样做的原因,这是不是因为一些编译器可能不总是初始化静态变量零(即没有做这样的初始化是晚期破任何编译器,并不能算得上是C或C ++编译器),它是说你的意思 - 可能是编程的最基本规则

I have a static variable declared but uninitialized in a function. Will this variable be initialized to zero automatically?

static int idx;

解决方案

Yes - the C standard ISO/IEC 9899:1999 a.k.a. C99 (and C++) standards say this must be so. See item 10 in section 6.7.8 ("Initialization") of WG14 N1256 for the exact text.

As others have pointed out, it is good practice to always initialise static variables:

static int idx = 0;

The reason for doing this is not because some compiler might not always initialise static variables to zero (any compiler that failed to do such initialisation would be terminally broken, and could not claim to be a C or C++ compiler), it is to Say What You Mean - possibly the most basic rule of programming.

这篇关于海湾合作委员会是否自动初始化静态变量为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆