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 7 5055
somenath wrote:
>
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 ?
"rank" is the missing word here.
To compare two expressions, they must be converted to the same type.
The rule for comparing two exressions is simple,
and signed or unsigned types do not form any exceptions to this rule:
The lower ranking type is converted to the higher ranking type.
The above text about comparisons
being machine dependent because of sizes, is wrong.
For example, suppose that int and long are both 32 bits.
Then 1L < 1U, because 1U, which is an unsigned int,
is *converted* (not promoted) to a signed long,
and it has absolutely nothing to do with whether or not
int and long are the same size.
"Integer promotions" on the other hand, which are distinct
from the kind of conversions performed on an operand
of a relational operator, are dependent upon whether
or not type int can represent all the positive values
of the lower ranking type.
N869
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
[#1] Every integer type has an integer conversion rank
defined as follows:
 No two signed integer types shall have the same rank,
even if they have the same representation.
 The rank of a signed integer type shall be greater than
the rank of any signed integer type with less
precision.
 The rank of long long int shall be greater than the
rank of long int, which shall be greater than the rank
of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed
char.
 The rank of any unsigned integer type shall equal the
rank of the corresponding signed integer type, if any.
 The rank of any standard integer type shall be greater
than the rank of any extended integer type with the
same width.
 The rank of char shall equal the rank of signed char
and unsigned char.
 The rank of _Bool shall be less than the rank of all
other standard integer types.
 The rank of any enumerated type shall equal the rank of
the compatible integer type (see 6.7.2.2).
 The rank of any extended signed integer type relative
to another extended signed integer type with the same
precision is implementationdefined, but still subject
to the other rules for determining the integer
conversion rank.
 For all integer types T1, T2, and T3, if T1 has greater
rank than T2 and T2 has greater rank than T3, then T1
has greater rank than T3.
/* BEGIN new.c */
#include<stdio. h>
#include<limits .h>
int main(void)
{
signed long sl = 5;
unsigned u = 5;
if (sl u) {
puts("5 is greater than 5");
} else {
puts("5 is less than 5");
}
printf("LONG_MA X is %ld\n", LONG_MAX);
printf("UINT_MA X is %u \n", UINT_MAX);
return 0;
}
/* END new.c */
On my machine the output of new.c is:
5 is greater than 5
LONG_MAX is 2147483647
UINT_MAX is 4294967295
The unsigned type is converted to the long signed type,
even though on my machine,
they have the same size and number of bits.

pete
On Jul 23, 12:37 pm, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
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 ?
"rank" is the missing word here.
To compare two expressions, they must be converted to the same type.
The rule for comparing two exressions is simple,
and signed or unsigned types do not form any exceptions to this rule:
The lower ranking type is converted to the higher ranking type.
The above text about comparisons
being machine dependent because of sizes, is wrong.
For example, suppose that int and long are both 32 bits.
Then 1L < 1U, because 1U, which is an unsigned int,
is *converted* (not promoted) to a signed long,
and it has absolutely nothing to do with whether or not
int and long are the same size.
"Integer promotions" on the other hand, which are distinct
from the kind of conversions performed on an operand
of a relational operator, are dependent upon whether
or not type int can represent all the positive values
of the lower ranking type.
N869
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
[#1] Every integer type has an integer conversion rank
defined as follows:
 No two signed integer types shall have the same rank,
even if they have the same representation.
 The rank of a signed integer type shall be greater than
the rank of any signed integer type with less
precision.
 The rank of long long int shall be greater than the
rank of long int, which shall be greater than the rank
of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed
char.
 The rank of any unsigned integer type shall equal the
rank of the corresponding signed integer type, if any.
 The rank of any standard integer type shall be greater
than the rank of any extended integer type with the
same width.
 The rank of char shall equal the rank of signed char
and unsigned char.
 The rank of _Bool shall be less than the rank of all
other standard integer types.
 The rank of any enumerated type shall equal the rank of
the compatible integer type (see 6.7.2.2).
 The rank of any extended signed integer type relative
to another extended signed integer type with the same
precision is implementationdefined, but still subject
to the other rules for determining the integer
conversion rank.
 For all integer types T1, T2, and T3, if T1 has greater
rank than T2 and T2 has greater rank than T3, then T1
has greater rank than T3.
/* BEGIN new.c */
#include<stdio. h>
#include<limits .h>
int main(void)
{
signed long sl = 5;
unsigned u = 5;
if (sl u) {
puts("5 is greater than 5");
} else {
puts("5 is less than 5");
}
printf("LONG_MA X is %ld\n", LONG_MAX);
printf("UINT_MA X is %u \n", UINT_MAX);
return 0;
}
/* END new.c */
On my machine the output of new.c is:
5 is greater than 5
LONG_MAX is 2147483647
UINT_MAX is 4294967295
The unsigned type is converted to the long signed type,
even though on my machine,
they have the same size and number of bits.
Hi Pete,
Many thanks for this information. But i am still not able to
understand the comparision.
As par my signed long has higher rank than unsigned int . so "u" will
be converted to
Signed long .Does it mean the value of "u" which is 5 will be
converted to 5 ?
Even then how "if (sl u)" is true ?
Regards,
Somenath
somenath wrote:
>
On Jul 23, 12:37 pm, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
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 ?
"rank" is the missing word here.
To compare two expressions, they must be converted to the same type.
The rule for comparing two exressions is simple,
and signed or unsigned types do not form any exceptions to this rule:
The lower ranking type is converted to the higher ranking type.
The above text about comparisons
being machine dependent because of sizes, is wrong.
For example, suppose that int and long are both 32 bits.
Then 1L < 1U, because 1U, which is an unsigned int,
is *converted* (not promoted) to a signed long,
and it has absolutely nothing to do with whether or not
int and long are the same size.
"Integer promotions" on the other hand, which are distinct
from the kind of conversions performed on an operand
of a relational operator, are dependent upon whether
or not type int can represent all the positive values
of the lower ranking type.
N869
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
[#1] Every integer type has an integer conversion rank
defined as follows:
 No two signed integer types shall have the same rank,
even if they have the same representation.
 The rank of a signed integer type shall be greater than
the rank of any signed integer type with less
precision.
 The rank of long long int shall be greater than the
rank of long int, which shall be greater than the rank
of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed
char.
 The rank of any unsigned integer type shall equal the
rank of the corresponding signed integer type, if any.
 The rank of any standard integer type shall be greater
than the rank of any extended integer type with the
same width.
 The rank of char shall equal the rank of signed char
and unsigned char.
 The rank of _Bool shall be less than the rank of all
other standard integer types.
 The rank of any enumerated type shall equal the rank of
the compatible integer type (see 6.7.2.2).
 The rank of any extended signed integer type relative
to another extended signed integer type with the same
precision is implementationdefined, but still subject
to the other rules for determining the integer
conversion rank.
 For all integer types T1, T2, and T3, if T1 has greater
rank than T2 and T2 has greater rank than T3, then T1
has greater rank than T3.
/* BEGIN new.c */
#include<stdio. h>
#include<limits .h>
int main(void)
{
signed long sl = 5;
unsigned u = 5;
if (sl u) {
puts("5 is greater than 5");
} else {
puts("5 is less than 5");
}
printf("LONG_MA X is %ld\n", LONG_MAX);
printf("UINT_MA X is %u \n", UINT_MAX);
return 0;
}
/* END new.c */
On my machine the output of new.c is:
5 is greater than 5
LONG_MAX is 2147483647
UINT_MAX is 4294967295
The unsigned type is converted to the long signed type,
even though on my machine,
they have the same size and number of bits.
Hi Pete,
Many thanks for this information. But i am still not able to
understand the comparision.
As par my signed long has higher rank than unsigned int . so "u" will
be converted to
Signed long .Does it mean the value of "u" which is 5 will be
converted to 5 ?
Even then how "if (sl u)" is true ?
The answer is simple: I was wrong.
The rules are more complicated.
Both sl and u get converted to type long unsigned in the
above expression on my machine.
The reason is because on my machine,
signed long can't represent the entire range of unsigned values.
The conversion in question,
is called "the usual arithmetic conversions".
N869
6.3.1.8 Usual arithmetic conversions
[#1] Many operators that expect operands of arithmetic type
cause conversions and yield result types in a similar way.
The purpose is to determine a common real type for the
operands and result. For the specified operands, each
operand is converted, without change of type domain, to a
type whose corresponding real type is the common real type.
Unless explicitly stated otherwise, the common real type is
also the corresponding real type of the result, whose type
domain is the type domain of the operands if they are the
same, and complex otherwise. This pattern is called the
usual arithmetic conversions:
First, if the corresponding real type of either operand
is long double, the other operand is converted, without
change of type domain, to a type whose corresponding
real type is long double.
Otherwise, if the corresponding real type of either
operand is double, the other operand is converted,
without change of type domain, to a type whose
corresponding real type is double.
Otherwise, if the corresponding real type of either
operand is float, the other operand is converted,
without change of type domain, to a type whose
corresponding real type is float.
Otherwise, the integer promotions are performed on both
operands. Then the following rules are applied to the
promoted operands:
If both operands have the same type, then no
further conversion is needed.
Otherwise, if both operands have signed integer
types or both have unsigned integer types, the
operand with the type of lesser integer conversion
rank is converted to the type of the operand with
greater rank.
Otherwise, if the operand that has unsigned
integer type has rank greater or equal to the rank
of the type of the other operand, then the operand
with signed integer type is converted to the type
of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed
integer type can represent all of the values of
the type of the operand with unsigned integer
type, then the operand with unsigned integer type
is converted to the type of the operand with
signed integer type.
Otherwise, both operands are converted to the
unsigned integer type corresponding to the type of
the operand with signed integer type.

pete
On Jul 25, 8:35 am, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
On Jul 23, 12:37 pm, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
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 ?
"rank" is the missing word here.
To compare two expressions, they must be converted to the same type.
The rule for comparing two exressions is simple,
and signed or unsigned types do not form any exceptions to this rule:
The lower ranking type is converted to the higher ranking type.
The above text about comparisons
being machine dependent because of sizes, is wrong.
For example, suppose that int and long are both 32 bits.
Then 1L < 1U, because 1U, which is an unsigned int,
is *converted* (not promoted) to a signed long,
and it has absolutely nothing to do with whether or not
int and long are the same size.
"Integer promotions" on the other hand, which are distinct
from the kind of conversions performed on an operand
of a relational operator, are dependent upon whether
or not type int can represent all the positive values
of the lower ranking type.
N869
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
[#1] Every integer type has an integer conversion rank
defined as follows:
 No two signed integer types shall have the same rank,
even if they have the same representation.
 The rank of a signed integer type shall be greater than
the rank of any signed integer type with less
precision.
 The rank of long long int shall be greater than the
rank of long int, which shall be greater than the rank
of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed
char.
 The rank of any unsigned integer type shall equal the
rank of the corresponding signed integer type, if any.
 The rank of any standard integer type shall be greater
than the rank of any extended integer type with the
same width.
 The rank of char shall equal the rank of signed char
and unsigned char.
 The rank of _Bool shall be less than the rank of all
other standard integer types.
 The rank of any enumerated type shall equal the rank of
the compatible integer type (see 6.7.2.2).
 The rank of any extended signed integer type relative
to another extended signed integer type with the same
precision is implementationdefined, but still subject
to the other rules for determining the integer
conversion rank.
 For all integer types T1, T2, and T3, if T1 has greater
rank than T2 and T2 has greater rank than T3, then T1
has greater rank than T3.
/* BEGIN new.c */
#include<stdio. h>
#include<limits .h>
int main(void)
{
signed long sl = 5;
unsigned u = 5;
if (sl u) {
puts("5 is greater than 5");
} else {
puts("5 is less than 5");
}
printf("LONG_MA X is %ld\n", LONG_MAX);
printf("UINT_MA X is %u \n", UINT_MAX);
return 0;
}
/* END new.c */
On my machine the output of new.c is:
5 is greater than 5
LONG_MAX is 2147483647
UINT_MAX is 4294967295
The unsigned type is converted to the long signed type,
even though on my machine,
they have the same size and number of bits.
Hi Pete,
Many thanks for this information. But i am still not able to
understand the comparision.
As par my signed long has higher rank than unsigned int . so "u" will
be converted to
Signed long .Does it mean the value of "u" which is 5 will be
converted to 5 ?
Even then how "if (sl u)" is true ?
The answer is simple: I was wrong.
The rules are more complicated.
Both sl and u get converted to type long unsigned in the
But does it mean 5 will be converted to 5 ? Even then how "if
(sl u)" is true ?
On Jul 25, 8:35 am, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
On Jul 23, 12:37 pm, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
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 ?
"rank" is the missing word here.
To compare two expressions, they must be converted to the same type.
The rule for comparing two exressions is simple,
and signed or unsigned types do not form any exceptions to this rule:
The lower ranking type is converted to the higher ranking type.
The above text about comparisons
being machine dependent because of sizes, is wrong.
For example, suppose that int and long are both 32 bits.
Then 1L < 1U, because 1U, which is an unsigned int,
is *converted* (not promoted) to a signed long,
and it has absolutely nothing to do with whether or not
int and long are the same size.
"Integer promotions" on the other hand, which are distinct
from the kind of conversions performed on an operand
of a relational operator, are dependent upon whether
or not type int can represent all the positive values
of the lower ranking type.
N869
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
[#1] Every integer type has an integer conversion rank
defined as follows:
 No two signed integer types shall have the same rank,
even if they have the same representation.
 The rank of a signed integer type shall be greater than
the rank of any signed integer type with less
precision.
 The rank of long long int shall be greater than the
rank of long int, which shall be greater than the rank
of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed
char.
 The rank of any unsigned integer type shall equal the
rank of the corresponding signed integer type, if any.
 The rank of any standard integer type shall be greater
than the rank of any extended integer type with the
same width.
 The rank of char shall equal the rank of signed char
and unsigned char.
 The rank of _Bool shall be less than the rank of all
other standard integer types.
 The rank of any enumerated type shall equal the rank of
the compatible integer type (see 6.7.2.2).
 The rank of any extended signed integer type relative
to another extended signed integer type with the same
precision is implementationdefined, but still subject
to the other rules for determining the integer
conversion rank.
 For all integer types T1, T2, and T3, if T1 has greater
rank than T2 and T2 has greater rank than T3, then T1
has greater rank than T3.
/* BEGIN new.c */
#include<stdio. h>
#include<limits .h>
int main(void)
{
signed long sl = 5;
unsigned u = 5;
if (sl u) {
puts("5 is greater than 5");
} else {
puts("5 is less than 5");
}
printf("LONG_MA X is %ld\n", LONG_MAX);
printf("UINT_MA X is %u \n", UINT_MAX);
return 0;
}
/* END new.c */
On my machine the output of new.c is:
5 is greater than 5
LONG_MAX is 2147483647
UINT_MAX is 4294967295
The unsigned type is converted to the long signed type,
even though on my machine,
they have the same size and number of bits.
Hi Pete,
Many thanks for this information. But i am still not able to
understand the comparision.
As par my signed long has higher rank than unsigned int . so "u" will
be converted to
Signed long .Does it mean the value of "u" which is 5 will be
converted to 5 ?
Even then how "if (sl u)" is true ?
The answer is simple: I was wrong.
The rules are more complicated.
Both sl and u get converted to type long unsigned in the
But does it mean 5 will be converted to 5 ? Even then how "if
(sl u)" is true ?
somenath wrote:
>
On Jul 25, 8:35 am, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
On Jul 23, 12:37 pm, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
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 ?
"rank" is the missing word here.
To compare two expressions, they must be converted to the same type.
The rule for comparing two exressions is simple,
and signed or unsigned types do not form any exceptions to this rule:
The lower ranking type is converted to the higher ranking type.
The above text about comparisons
being machine dependent because of sizes, is wrong.
For example, suppose that int and long are both 32 bits.
Then 1L < 1U, because 1U, which is an unsigned int,
is *converted* (not promoted) to a signed long,
and it has absolutely nothing to do with whether or not
int and long are the same size.
"Integer promotions" on the other hand, which are distinct
from the kind of conversions performed on an operand
of a relational operator, are dependent upon whether
or not type int can represent all the positive values
of the lower ranking type.
N869
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
[#1] Every integer type has an integer conversion rank
defined as follows:
 No two signed integer types shall have the same rank,
even if they have the same representation.
 The rank of a signed integer type shall be greater than
the rank of any signed integer type with less
precision.
 The rank of long long int shall be greater than the
rank of long int, which shall be greater than the rank
of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed
char.
 The rank of any unsigned integer type shall equal the
rank of the corresponding signed integer type, if any.
 The rank of any standard integer type shall be greater
than the rank of any extended integer type with the
same width.
 The rank of char shall equal the rank of signed char
and unsigned char.
 The rank of _Bool shall be less than the rank of all
other standard integer types.
 The rank of any enumerated type shall equal the rank of
the compatible integer type (see 6.7.2.2).
 The rank of any extended signed integer type relative
to another extended signed integer type with the same
precision is implementationdefined, but still subject
to the other rules for determining the integer
conversion rank.
 For all integer types T1, T2, and T3, if T1 has greater
rank than T2 and T2 has greater rank than T3, then T1
has greater rank than T3.
/* BEGIN new.c */
#include<stdio. h>
#include<limits .h>
int main(void)
{
signed long sl = 5;
unsigned u = 5;
if (sl u) {
puts("5 is greater than 5");
} else {
puts("5 is less than 5");
}
printf("LONG_MA X is %ld\n", LONG_MAX);
printf("UINT_MA X is %u \n", UINT_MAX);
return 0;
}
/* END new.c */
On my machine the output of new.c is:
5 is greater than 5
LONG_MAX is 2147483647
UINT_MAX is 4294967295
The unsigned type is converted to the long signed type,
even though on my machine,
they have the same size and number of bits.
Hi Pete,
Many thanks for this information. But i am still not able to
understand the comparision.
As par my signed
long has higher rank than unsigned int . so "u" will
be converted to
Signed long .Does it mean the value of "u" which is 5 will be
converted to 5 ?
Even then how "if (sl u)" is true ?
The answer is simple: I was wrong.
The rules are more complicated.
Both sl and u get converted to type long unsigned in the
But does it mean 5 will be converted to 5 ?
No. The converted expression is equal to ((long unsigned)5).
Even then how "if
(sl u)" is true ?
The rule for converting a negative value to an unsigned integer type
is to add (1 + UNSIGNED_INTEGE R_TYPE_MAX) repeatedly
to the negative value until it is not negative any more.
5 gets converted to 4294967291LU

pete
On Jul 25, 3:35 pm, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
On Jul 25, 8:35 am, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
On Jul 23, 12:37 pm, pete <pfil...@mindsp ring.comwrote:
somenath wrote:
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 ?
"rank" is the missing word here.
To compare two expressions, they must be converted to the same type.
The rule for comparing two exressions is simple,
and signed or unsigned types do not form any exceptions to this rule:
The lower ranking type is converted to the higher ranking type.
The above text about comparisons
being machine dependent because of sizes, is wrong.
For example, suppose that int and long are both 32 bits.
Then 1L < 1U, because 1U, which is an unsigned int,
is *converted* (not promoted) to a signed long,
and it has absolutely nothing to do with whether or not
int and long are the same size.
"Integer promotions" on the other hand, which are distinct
from the kind of conversions performed on an operand
of a relational operator, are dependent upon whether
or not type int can represent all the positive values
of the lower ranking type.
N869
6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
[#1] Every integer type has an integer conversion rank
defined as follows:
 No two signed integer types shall have the same rank,
even if they have the same representation.
 The rank of a signed integer type shall be greater than
the rank of any signed integer type with less
precision.
 The rank of long long int shall be greater than the
rank of long int, which shall be greater than the rank
of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed
char.
 The rank of any unsigned integer type shall equal the
rank of the corresponding signed integer type, if any.
 The rank of any standard integer type shall be greater
than the rank of any extended integer type with the
same width.
 The rank of char shall equal the rank of signed char
and unsigned char.
 The rank of _Bool shall be less than the rank of all
other standard integer types.
 The rank of any enumerated type shall equal the rank of
the compatible integer type (see 6.7.2.2).
 The rank of any extended signed integer type relative
to another extended signed integer type with the same
precision is implementationdefined, but still subject
to the other rules for determining the integer
conversion rank.
 For all integer types T1, T2, and T3, if T1 has greater
rank than T2 and T2 has greater rank than T3, then T1
has greater rank than T3.
/* BEGIN new.c */
#include<stdio. h>
#include<limits .h>
int main(void)
{
signed long sl = 5;
unsigned u = 5;
if (sl u) {
puts("5 is greater than 5");
} else {
puts("5 is less than 5");
}
printf("LONG_MA X is %ld\n", LONG_MAX);
printf("UINT_MA X is %u \n", UINT_MAX);
return 0;
}
/* END new.c */
On my machine the output of new.c is:
5 is greater than 5
LONG_MAX is 2147483647
UINT_MAX is 4294967295
The unsigned type is converted to the long signed type,
even though on my machine,
they have the same size and number of bits.
Hi Pete,
Many thanks for this information. But i am still not able to
understand the comparision.
As par my signed
long has higher rank than unsigned int . so "u" will
be converted to
Signed long .Does it mean the value of "u" which is 5 will be
converted to 5 ?
Even then how "if (sl u)" is true ?
The answer is simple: I was wrong.
The rules are more complicated.
Both sl and u get converted to type long unsigned in the
But does it mean 5 will be converted to 5 ?
No. The converted expression is equal to ((long unsigned)5).
Even then how "if
(sl u)" is true ?
The rule for converting a negative value to an unsigned integer type
is to add (1 + UNSIGNED_INTEGE R_TYPE_MAX) repeatedly
to the negative value until it is not negative any more.
5 gets converted to 4294967291LU
But is this rules applies to largest negative number such as
2147483648 ? then what will be the result when it is converted to
unsigned integer type? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics 
by: Fred Ma 
last post by:
Hello,
I've been trying to clear up a confusion about
integer promotions during expression evaluation.
I've checked the C FAQ and C++ FAQ (they are
different languages, but I was hoping one would
clear up the confusion), as well as googling
groups and the web.
The confusion is that for a binary operator,

by: TTroy 
last post by:
Hello, I'm relatively new to C and have gone through more than 4 books
on it. None mentioned anything about integral promotion, arithmetic
conversion, value preserving and unsigned preserving. And K&R2
mentions "signed extension" everywhere.
Reading some old clc posts, I've beginning to realize that these books
are overgeneralizing the topic. I am just wondering what the
difference between the following pairs of terms are:
1)...

by: G Fernandes 
last post by:
Hello,
I have a question about using an unsigned int and a signed long int in
an expression (where UINT_MAX > LINT_MAX).
Based on the conversion rules, the unsigned int will be convered to
long int, but what if it can't fit? Is this undefined behaviour?
Any help will be appreciated.

by: junky_fellow 
last post by:
Can anybody please explain this:
When a value with integer type is converted to another integer type
other than _Bool,
if the new type is unsigned, the value is converted by repeatedly
adding or subtracting one more than the maximum value that can be
represented in the new type until the value is in the range of the new
type.

by: Gilles Rochefort 
last post by:
Hello,
I wrote the following code to wrap some existing C functions, and I get
some warning about converting float to int.
float zfactor;
int tmp_x, corners_x;
std::transform(tmp_x,tmp_x+4,corners_x, bind2nd( multiplies<float>(),zfactor) );
 
by: sarathy 
last post by:
Hi,
What is integer promotion? How is it different from
arithmetic conversion?
Regards,
Sarathy

by: NM 
last post by:
I am given an int and have to tell whether the bit representation of that
int is a palindrome or not.
Here is what I have come up with
bool isIntPalindrome(int num)
{
unsigned int temp = num;
int reversed = 0;
while (num != 0) {

by: techie 
last post by:
I have defined a number of unsigned integer types as follows:
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedfe long long uint64;
Is it necessary to explicitly cast from one type of unsigned integer type to
another even though they do so implicitly?

by: Peter Lee 
last post by:
what's the correct behaver about the following code ? ( C++
standard )
I got a very strange result....
class MyClass
{
public:
MyClass(const char* p)
{
printf("ctor p=%s\n", p);

by: marktang 
last post by:
ONU (Optical Network Unit) is one of the key components for providing highspeed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...

by: Hystou 
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
 
by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++12 std=c++20 Wnarrowing bit_field.cpp
Here is the code in...

by: Hystou 
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...

by: agi2029 
last post by:
Let's talk about the concept of autonomous AI software engineers and nocode agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...

by: conductexam 
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...

by: TSSRALBI 
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LANtoLAN VPNs.
The last exercise I practiced was to create a LANtoLAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.
 
by: bsmnconsultancy 
last post by:
In today's digital era, a welldesigned website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
 