#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?
CodePudding user response:
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.
CodePudding user response:
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.
CodePudding user response:
Because:
- If
c > 120
istrue
, thenc > 110
is also true. The same thing happens. Ifc < 120
istrue
thenc < 200
is alsotrue
. - You define
c
as achar
which is asigned char
. The biggest value that asigned char
can hold is127
. So no matter the value,c < 200
is alwaystrue
Also, please do some proper block scope and identation. If you don't, it will come back to bite you later.