Self-Initialization

遇到一段代码,觉得很迷糊,觉得不对。于是做了个测试,代码如下

#include <stdio.h>int main(){        int flag = flag ;//flag哪里定义?        flag = 1;        printf("flag is %d\n",flag);}

在gcc下竟然可以通过,输出也是对的。

如果不赋值的话,flag输出的为无意义值

于是产生为什么flag不提示未定义的疑问,这是什么语法?gcc连警告都不提。

水木上的牛人给出了网址

http://cpp-style.info/Addison.Wesley-CPP.Gotchas-Avoiding.Common.Problems.in.Coding.and.Design/0321125185_ch02lev1sec9.html

说是 Self-Initialization,解答上有如下的话

/*It's undefined. In C++, a name comes into scope before its initializer is parsed, so any reference to the name within the initializer refers to the name being declared! Not many programmers will compose as strange a declaration as the one above, but it's possible to cut and paste your way into trouble*/

翻译出来就是,该行为未定义。

原文链接: https://www.cnblogs.com/westfly/archive/2011/04/02/2003881.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/23261

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午1:13
下一篇 2023年2月8日 上午1:14

相关推荐