I ran this short test on VC++ 2010 Express and Code::Blocks
int main() { if ( -1 < ( unsigned char ) 1 ) printf("true 1\n"); if ( -1 < ( unsigned short ) 1 ) printf("true 2\n"); if ( -1 < ( unsigned int ) 1 ) printf("true 3\n"); if ( -1 < 1 ) printf("true 4\n"); }
and got the following results
I understood it clearly now.
unsigned char ch = 1;
if ( -1 < ch )
the ch will be converted to int ,so -1 and 1 both are integers and hence the condition is true.
if ( -1 < (unsigned char ) 1 )
same as above. and same for short int.
if ( -1 < (unsigned int ) 1 )
now lhs (-1) is int and rhs (1) is unsigned , so -1 will be converted to unsigned int, giving the biggest value.
hence the condition is false.
please correct me if i am wrong.