what is the behavior of a statement logically operated with 0?
-
ex- if( 0 < -1 ) I remember "C" compiler takes bits data of -1 to signed format and returns true for the above statement. Whats behavior in C++. Is the behavior same/different.Any updated standard(C/C++) brought changes to this behavior.Please answer.
-
ex- if( 0 < -1 ) I remember "C" compiler takes bits data of -1 to signed format and returns true for the above statement. Whats behavior in C++. Is the behavior same/different.Any updated standard(C/C++) brought changes to this behavior.Please answer.
In
C
a zero value is assigned to(0 < -1)
expression, hence, in the following codeif ( 0 < -1)
{
k = 5;
}
else
{
k = 10;
}the statement
k = 10;
is executed. InC++
the esame expression is evaluated asfalse
and (like happened in theC
program) the statementk = 10;
is executed.Veni, vidi, vici.
-
In
C
a zero value is assigned to(0 < -1)
expression, hence, in the following codeif ( 0 < -1)
{
k = 5;
}
else
{
k = 10;
}the statement
k = 10;
is executed. InC++
the esame expression is evaluated asfalse
and (like happened in theC
program) the statementk = 10;
is executed.Veni, vidi, vici.
I tried on gcc compiler, the statement 0 < -1 always false.please help.
-
I tried on gcc compiler, the statement 0 < -1 always false.please help.
-
Of course. I made a blunder. :( I am very sorry. Please see my (hopefully) fixed answer.
Veni, vidi, vici.
Oh.. fine..But I read somewhere that when a signed value is compared with unsigned, signed converted to unsigned first. 0 is by default unsigned, hence signed version of -1 is greater than 0. May be old compiler showing this behavior.Do u have any idea?
-
Oh.. fine..But I read somewhere that when a signed value is compared with unsigned, signed converted to unsigned first. 0 is by default unsigned, hence signed version of -1 is greater than 0. May be old compiler showing this behavior.Do u have any idea?
Oh, now I see. You are looking for an example of 'idiosyncrasy of integer promotions' (see, for instance, , however
shaktikanta wrote:
0 is by default unsigned
is a wrong assumption. It is signed, by default. You have to write:
#include <stdio.h>
int main()
{
int k;
if ( 0u < -1)
{
k=5;
}
else
{
k=10;
}
printf("%d\n", k);
return 0;
}Veni, vidi, vici.