hi,

how the below code is generating out put as " -1 < ( unsigned char ) 1 ".

i read in many books saying when we do operation with singed and unsigned value always the resultant will be data type that has more width .

i understand that -1 when compared with unsigned should be coverted to unsigened int and yeild a big value than 1.
but the out put is different.

whats the story..
please help.

main() {
  if ( -1 < (unsigned char) 1 )
      printf("-1 is less than (unsigned char) 1");
  else
      printf("-1 NOT less than (unsigned char) 1");
}

why would -1 be converted to unsigned int when (unsigned char) 1 can just be converted to int which has more width than unsigned char and is already the type of -1?

Did you mean to compare -1 and 1U?

why would -1 be converted to unsigned int when (unsigned char) 1 can just be converted to int which has more width than unsigned char and is already the type of -1?

Did you mean to compare -1 and 1U?

i dont understand what does the if understand below statements as

if ( -1 < ( unsigned char ) 1 )
   if ( -1 < ( unsigned short ) 1 )
   if ( -1 < ( unsigned int ) 1 )  
   if ( -1 <  1 )

and when -1 gets converted to unsigned.

In lines 2 and 3 "1" can be promoted to int

in line 4 "1" can be converted to int

in line 5 "1" is already an int

In all cases the compiler compares the values using the int type, -1 is not converted to unsigned.

Actually I just checked the standard I will get back to you about line 4.

Sorry I am wrong about line 4, the compiler tries the available promotions then because the lhs is still unsigned it converts the rhs to unsigned and the comparison is performed on signed ints.

The expression in line 4 is false where as all the other expressions are true.

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

true 1
true 2
true 4
Press any key to continue . . .

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.