Reputation:
#include <stdio.h>
int main()
{
char c='w';
if((c>110 && c>120)&&(c<120 && c<200))
printf("true");
else
printf("false");
return 0;
}
This is my code. The code does give output, but vscode gives me a warning that the comparison of characters with constants(like 200, it specifically shows this only for 200), will always be true, but it does execute the code successfully and gives the final result, which is "false". I tried the same code on the on an online compiler, here, the online compiler gives the result without any warnings.
Any reason for this?
Upvotes: 2
Views: 256
Reputation: 4663
There is this problem: You define c
as a char
which is a signed char
. The biggest value that a signed char
can hold is 127
. So no matter the value, c < 200
is always true
.
Also, if c > 120
is true
, then c > 110
is also true. The same thing happens. If c < 120
is true
then c < 200
is also true
.
Also, please do some proper block scope and indentation. If you don't, it will come back to bite you later.
Upvotes: 0
Reputation: 12610
You have defined c
as a char
, which is a signed char
as most compilers do by default. The highest value a signed char
can take is 127 on common systems. This is always less than 200, and the IDE tells you this fact.
Note: The header file "limits.h" defines minimum and maximum values for standard types. The interesting value is CHAR_MAX
that equals SCHAR_MAX
in your case.
Warnings by an IDE can differ from warnings by compilers, because they are different beasts. Some are "smarter" than others, however, not producing a warning does not mean that a construct is correct.
Upvotes: 3
Reputation: 10165
If c
is larger than 120, it is always larger than 110. Same is true with 120 and 200. Remove the useless comparison and the warning should disappear.
Upvotes: 1