Nostradamus
Nostradamus

Reputation: 172

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?

Upvotes: 0

Views: 86

Answers (2)

zwol
zwol

Reputation: 140669

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.

Upvotes: 2

Eric Postpischil
Eric Postpischil

Reputation: 222942

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.

Upvotes: 1

Related Questions