int i = 5;
while(i>5)
printf("%d",i);
prints nothing.
int i = 5;
while ( 5<i<10 )
{
printf("%d",i);
i++;
}
prints 5
In both the cases shouldn't be the result "prints nothing" . Because 5 is not less than 5.
In C integer is used as a boolean: 0
is false
, everything else is true
.
As @JonathanLeffler noted (see his comment below), in C99 and C11 there is a standard boolean datatype, but it also expands to integer constants (0
and 1
). Link.
When you write an expression like 5 < i < 10
, it is treated like (5 < i) < 10
, where 5 < i
is a boolean expression, which returns 0
. So you get (0) < 10
, which is true, that's why the loop's body is executed.
In order to make the loop condition correct, you shoould use something like:
while (5 < i && i < 10)