简体   繁体   中英

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. The line of code is: while(--argc>-0) . I compiled the program with make. Why doesn't a warning appear when -0 is written in the code?

You may not realize that integer constants in C cannot be negative. -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; an angry mob of mathematicians would assault the C committee if it wasn't defined that way. So, the C compiler doesn't think this is a mistake.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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