Nav*_*K N 9 c compiler-warnings visual-c++ c4127
对于代码,
while(1)
{
/* ..... */
}
Run Code Online (Sandbox Code Playgroud)
MSVC生成以下警告.
warning C4127: conditional expression is constant
Run Code Online (Sandbox Code Playgroud)
警告的MSDN页面建议使用for(;;)而不是while(1).我想知道它有什么优势for(;;)以及为什么它会在不断使用中发出警告while?
我应该在GCC上使用什么标志来获得相同的警告?
Nor*_*ame 14
恒定条件通常就足够了.考虑一下:
unsigned k;
...
while (k>=0)
{
...
}
Run Code Online (Sandbox Code Playgroud)
k>=0如果k是带符号的int,则条件有意义,但对于unsigned则不然.粗心的开发人员忘记了k被声明为无符号,并且他/她会使用它,好像它可用作负数.编译器试图提供帮助并向您发出警告 while(1),并将编译器放入同一个问题类中.for(;;)是优选的,因为它毫不含糊地意味着`循环永远