S
somenath
Hi All,
I am trying to undestand "Type Conversions" from K&R book.I am not
able to understand the
bellow mentioned text
"Conversion rules are more complicated when unsigned operands are
involved. The problem
is that comparisons between signed and unsigned values are machine-
dependent, because
they depend on the sizes of the various integer types. For example,
suppose that int is 16 bits
and long is 32 bits. Then -1L < 1U, because 1U, which is an unsigned
int, is promoted to a
signed long. But -1L > 1UL because -1L is promoted to unsigned long
and thus appears
to be a large positive number"
To understand it i wrote a small code
#include<stdio.h>
int main(void)
{
signed long sl = -5;
unsigned long ul= 5;
if (sl>ul)
{
printf("\n -5 is greater than 5\n");
}
else
{
printf("-5 is less than 5 \n");
}
return 0;
}
OP is
-5 is greater than 5
Could anybody help me to understand the conversion rules when
unsigned operands are involved ?
Regards,
Somenath
I am trying to undestand "Type Conversions" from K&R book.I am not
able to understand the
bellow mentioned text
"Conversion rules are more complicated when unsigned operands are
involved. The problem
is that comparisons between signed and unsigned values are machine-
dependent, because
they depend on the sizes of the various integer types. For example,
suppose that int is 16 bits
and long is 32 bits. Then -1L < 1U, because 1U, which is an unsigned
int, is promoted to a
signed long. But -1L > 1UL because -1L is promoted to unsigned long
and thus appears
to be a large positive number"
To understand it i wrote a small code
#include<stdio.h>
int main(void)
{
signed long sl = -5;
unsigned long ul= 5;
if (sl>ul)
{
printf("\n -5 is greater than 5\n");
}
else
{
printf("-5 is less than 5 \n");
}
return 0;
}
OP is
-5 is greater than 5
Could anybody help me to understand the conversion rules when
unsigned operands are involved ?
Regards,
Somenath