简体   繁体   English

为什么在代码中写-0时没有出现警告?

[英]Why doesn't a warning appear when -0 is written in the code?

I was testing a code and I accidentally put -0 in a line of code and noticed that no warning appears.我正在测试一个代码,我不小心在一行代码中输入了 -0 并注意到没有出现任何警告。 The line of code is: while(--argc>-0) .代码行是: while(--argc>-0) I compiled the program with make.我用make编译了程序。 Why doesn't a warning appear when -0 is written in the code?为什么在代码中写-0时没有出现警告?

You may not realize that integer constants in C cannot be negative.您可能没有意识到 C 中的 integer 常量不能为负数。 -0 is parsed as two tokens, the unary minus operator - and the integer constant 0. Applying unary minus to zero is well-defined, it just produces zero again; -0被解析为两个标记,一元减号运算符-和 integer 常量 0。将一元减号应用于零是明确定义的,它只会再次产生零; an angry mob of mathematicians would assault the C committee if it wasn't defined that way.如果没有这样定义,一群愤怒的数学家会攻击 C 委员会。 So, the C compiler doesn't think this is a mistake.所以,C 编译器不认为这是一个错误。

However, if you wanted a warning because you meant to write while(--argc>=0) , go ahead and file a feature request on your C compiler.但是,如果您想要一个警告,因为您打算编写while(--argc>=0) , go 并在您的 C 编译器上提交功能请求。 Typo detection has been getting popular lately, they might well like the idea.错字检测最近越来越流行,他们可能会喜欢这个主意。

You do not get a warning because this is well defined C code and experience has not shown code like this is indicates programmer error often enough to be worth warning about.您不会收到警告,因为这是定义明确的 C 代码,并且经验没有显示这样的代码,这表明程序员错误经常足以值得警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM