Conversion rules between unsigned operands and signed operand

Discussion in 'C Programming' started by somenath, Jul 23, 2007.

  1. somenath

    somenath Guest

    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
     
    somenath, Jul 23, 2007
    #1
    1. Advertising

  2. somenath

    pete Guest

    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 implementation-defined, 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_MAX is %ld\n", LONG_MAX);
    printf("UINT_MAX 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
     
    pete, Jul 23, 2007
    #2
    1. Advertising

  3. somenath

    somenath Guest

    Re: Conversion rules between unsigned operands and signed operand

    On Jul 23, 12:37 pm, pete <> wrote:
    > 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 implementation-defined, 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_MAX is %ld\n", LONG_MAX);
    > printf("UINT_MAX 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, Jul 24, 2007
    #3
  4. somenath

    pete Guest

    Re: Conversion rules between unsigned operands and signed operand

    somenath wrote:
    >
    > On Jul 23, 12:37 pm, pete <> wrote:
    > > 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 implementation-defined, 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_MAX is %ld\n", LONG_MAX);
    > > printf("UINT_MAX 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
     
    pete, Jul 25, 2007
    #4
  5. somenath

    somenath Guest

    Re: Conversion rules between unsigned operands and signed operand

    On Jul 25, 8:35 am, pete <> wrote:
    > somenath wrote:
    >
    > > On Jul 23, 12:37 pm, pete <> wrote:
    > > > 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 implementation-defined, 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_MAX is %ld\n", LONG_MAX);
    > > > printf("UINT_MAX 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, Jul 25, 2007
    #5
  6. somenath

    somenath Guest

    Re: Conversion rules between unsigned operands and signed operand

    On Jul 25, 8:35 am, pete <> wrote:
    > somenath wrote:
    >
    > > On Jul 23, 12:37 pm, pete <> wrote:
    > > > 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 implementation-defined, 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_MAX is %ld\n", LONG_MAX);
    > > > printf("UINT_MAX 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, Jul 25, 2007
    #6
  7. somenath

    pete Guest

    Re: Conversion rules between unsigned operands and signed operand

    somenath wrote:
    >
    > On Jul 25, 8:35 am, pete <> wrote:
    > > somenath wrote:
    > >
    > > > On Jul 23, 12:37 pm, pete <> wrote:
    > > > > 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 implementation-defined, 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_MAX is %ld\n", LONG_MAX);
    > > > > printf("UINT_MAX 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_INTEGER_TYPE_MAX) repeatedly
    to the negative value until it is not negative any more.

    -5 gets converted to 4294967291LU

    --
    pete
     
    pete, Jul 25, 2007
    #7
  8. somenath

    somenath Guest

    Re: Conversion rules between unsigned operands and signed operand

    On Jul 25, 3:35 pm, pete <> wrote:
    > somenath wrote:
    >
    > > On Jul 25, 8:35 am, pete <> wrote:
    > > > somenath wrote:

    >
    > > > > On Jul 23, 12:37 pm, pete <> wrote:
    > > > > > 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 implementation-defined, 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_MAX is %ld\n", LONG_MAX);
    > > > > > printf("UINT_MAX 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_INTEGER_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?
     
    somenath, Jul 30, 2007
    #8
  9. somenath

    pete Guest

    Re: Conversion rules between unsigned operands and signed operand

    somenath wrote:
    >
    > On Jul 25, 3:35 pm, pete <> wrote:


    > > The rule for converting a negative value to an unsigned integer type
    > > is to add (1 + UNSIGNED_INTEGER_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 ?


    I have some shocking news for you.
    On some implementations of C, like mine and probably yours,
    -2147483648 is a positive quantity.
    (-2147483648) is the result of the unary minus operator
    operating on a value of 2147483648.
    On my machine, 2147483648 excedes LONG_MAX,
    so it is converted to long unsigned.
    2147483648 equals -2147483648, on my C implementation.
    INT_MIN probably expands to (-2147483647 - 1)
    on your machine, like it does on mine.

    > then what will be the result when it is converted to
    > unsigned integer type?


    This is the output on my machine:

    /* BEGIN new.c output */

    -2147483648 is greater than zero.

    (-2147483647 - 1) is less than zero.

    -2147483648 == 2147483648

    (long unsigned)-5 is 4294967291
    (long unsigned)-2147483648 is 2147483648

    LONG_MAX is 2147483647
    UINT_MAX is 4294967295
    INT_MIN expands to (-2147483647 - 1)

    /* END new.c output */




    /* BEGIN new.c */

    #include<stdio.h>
    #include<limits.h>

    #define str(s) # s
    #define xstr(s) str(s)

    int main(void)
    {
    puts("/* BEGIN new.c output */\n");
    if (-2147483648 > 0) {
    puts("-2147483648 is greater than zero.\n");
    }
    if (0 > (-2147483647 - 1)) {
    puts("(-2147483647 - 1) is less than zero.\n");
    }
    if (-2147483648 == 2147483648) {
    puts("-2147483648 == 2147483648\n");
    }
    printf("(long unsigned)-5 is %lu\n",
    (long unsigned)-5);
    printf("(long unsigned)-2147483648 is %lu\n\n",
    (long unsigned)-2147483648);
    printf("LONG_MAX is %ld\n", LONG_MAX);
    printf("UINT_MAX is %u \n", UINT_MAX);
    puts("INT_MIN expands to " xstr(INT_MIN));
    puts("\n/* END new.c output */");
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Jul 30, 2007
    #9
  10. somenath

    pete Guest

    Re: Conversion rules between unsigned operands and signed operand

    pete wrote:
    >
    > somenath wrote:
    > >
    > > On Jul 25, 3:35 pm, pete <> wrote:

    >
    > > > The rule for converting a negative value to an unsigned integer type
    > > > is to add (1 + UNSIGNED_INTEGER_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 ?

    >
    > I have some shocking news for you.
    > On some implementations of C, like mine and probably yours,
    > -2147483648 is a positive quantity.
    > (-2147483648) is the result of the unary minus operator
    > operating on a value of 2147483648.
    > On my machine, 2147483648 excedes LONG_MAX,
    > so it is converted to long unsigned.


    Not actually converted;
    The original type of 2147483648, is long unsigned on my machine.

    > 2147483648 equals -2147483648, on my C implementation.
    > INT_MIN probably expands to (-2147483647 - 1)
    > on your machine, like it does on mine.
    >
    > > then what will be the result when it is converted to
    > > unsigned integer type?

    >
    > This is the output on my machine:
    >
    > /* BEGIN new.c output */
    >
    > -2147483648 is greater than zero.
    >
    > (-2147483647 - 1) is less than zero.
    >
    > -2147483648 == 2147483648
    >
    > (long unsigned)-5 is 4294967291
    > (long unsigned)-2147483648 is 2147483648
    >
    > LONG_MAX is 2147483647
    > UINT_MAX is 4294967295
    > INT_MIN expands to (-2147483647 - 1)
    >
    > /* END new.c output */
    >
    > /* BEGIN new.c */
    >
    > #include<stdio.h>
    > #include<limits.h>
    >
    > #define str(s) # s
    > #define xstr(s) str(s)
    >
    > int main(void)
    > {
    > puts("/* BEGIN new.c output */\n");
    > if (-2147483648 > 0) {
    > puts("-2147483648 is greater than zero.\n");
    > }
    > if (0 > (-2147483647 - 1)) {
    > puts("(-2147483647 - 1) is less than zero.\n");
    > }
    > if (-2147483648 == 2147483648) {
    > puts("-2147483648 == 2147483648\n");
    > }
    > printf("(long unsigned)-5 is %lu\n",
    > (long unsigned)-5);
    > printf("(long unsigned)-2147483648 is %lu\n\n",
    > (long unsigned)-2147483648);
    > printf("LONG_MAX is %ld\n", LONG_MAX);
    > printf("UINT_MAX is %u \n", UINT_MAX);
    > puts("INT_MIN expands to " xstr(INT_MIN));
    > puts("\n/* END new.c output */");
    > return 0;
    > }
    >
    > /* END new.c */
    >
    > --
    > pete


    --
    pete
     
    pete, Jul 30, 2007
    #10
  11. somenath

    somenath Guest

    Re: Conversion rules between unsigned operands and signed operand

    On Jul 30, 10:10 am, pete <> wrote:
    > pete wrote:
    >
    > > somenath wrote:

    >
    > > > On Jul 25, 3:35 pm, pete <> wrote:

    >
    > > > > The rule for converting a negative value to an unsigned integer type
    > > > > is to add (1 + UNSIGNED_INTEGER_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 ?

    >
    > > I have some shocking news for you.
    > > On some implementations of C, like mine and probably yours,
    > > -2147483648 is a positive quantity.
    > > (-2147483648) is the result of the unary minus operator
    > > operating on a value of 2147483648.
    > > On my machine, 2147483648 excedes LONG_MAX,
    > > so it is converted to long unsigned.

    >
    > Not actually converted;
    > The original type of 2147483648, is long unsigned on my machine.
    >
    >
    >
    >
    >
    > > 2147483648 equals -2147483648, on my C implementation.
    > > INT_MIN probably expands to (-2147483647 - 1)
    > > on your machine, like it does on mine.

    >
    > > > then what will be the result when it is converted to
    > > > unsigned integer type?

    >
    > > This is the output on my machine:

    >
    > > /* BEGIN new.c output */

    >
    > > -2147483648 is greater than zero.

    >
    > > (-2147483647 - 1) is less than zero.

    >
    > > -2147483648 == 2147483648

    >
    > > (long unsigned)-5 is 4294967291
    > > (long unsigned)-2147483648 is 2147483648

    >
    > > LONG_MAX is 2147483647
    > > UINT_MAX is 4294967295
    > > INT_MIN expands to (-2147483647 - 1)

    >
    > > /* END new.c output */

    >
    > > /* BEGIN new.c */

    >
    > > #include<stdio.h>
    > > #include<limits.h>

    >
    > > #define str(s) # s
    > > #define xstr(s) str(s)

    >
    > > int main(void)
    > > {
    > > puts("/* BEGIN new.c output */\n");
    > > if (-2147483648 > 0) {
    > > puts("-2147483648 is greater than zero.\n");
    > > }
    > > if (0 > (-2147483647 - 1)) {
    > > puts("(-2147483647 - 1) is less than zero.\n");
    > > }
    > > if (-2147483648 == 2147483648) {
    > > puts("-2147483648 == 2147483648\n");
    > > }
    > > printf("(long unsigned)-5 is %lu\n",
    > > (long unsigned)-5);
    > > printf("(long unsigned)-2147483648 is %lu\n\n",
    > > (long unsigned)-2147483648);
    > > printf("LONG_MAX is %ld\n", LONG_MAX);
    > > printf("UINT_MAX is %u \n", UINT_MAX);
    > > puts("INT_MIN expands to " xstr(INT_MIN));
    > > puts("\n/* END new.c output */");
    > > return 0;
    > > }

    >
    > > /* END new.c */

    >
    > > --
    > > pete

    >
    > --




    But does it mean "converting a negative value to an unsigned integer
    type " is implementation specific ? does c99 standard say anything
    about this ?
     
    somenath, Jul 30, 2007
    #11
  12. somenath

    pete Guest

    Re: Conversion rules between unsigned operands and signed operand

    somenath wrote:
    >
    > On Jul 30, 10:10 am, pete <> wrote:
    > > pete wrote:
    > >
    > > > somenath wrote:

    > >
    > > > > On Jul 25, 3:35 pm, pete <> wrote:

    > >
    > > > > > The rule for converting a negative value to an unsigned integer type
    > > > > > is to add (1 + UNSIGNED_INTEGER_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 ?

    > >
    > > > I have some shocking news for you.
    > > > On some implementations of C, like mine and probably yours,
    > > > -2147483648 is a positive quantity.
    > > > (-2147483648) is the result of the unary minus operator
    > > > operating on a value of 2147483648.
    > > > On my machine, 2147483648 excedes LONG_MAX,
    > > > so it is converted to long unsigned.

    > >
    > > Not actually converted;
    > > The original type of 2147483648, is long unsigned on my machine.
    > >
    > >
    > >
    > >
    > >
    > > > 2147483648 equals -2147483648, on my C implementation.
    > > > INT_MIN probably expands to (-2147483647 - 1)
    > > > on your machine, like it does on mine.

    > >
    > > > > then what will be the result when it is converted to
    > > > > unsigned integer type?

    > >
    > > > This is the output on my machine:

    > >
    > > > /* BEGIN new.c output */

    > >
    > > > -2147483648 is greater than zero.

    > >
    > > > (-2147483647 - 1) is less than zero.

    > >
    > > > -2147483648 == 2147483648

    > >
    > > > (long unsigned)-5 is 4294967291
    > > > (long unsigned)-2147483648 is 2147483648

    > >
    > > > LONG_MAX is 2147483647
    > > > UINT_MAX is 4294967295
    > > > INT_MIN expands to (-2147483647 - 1)

    > >
    > > > /* END new.c output */

    > >
    > > > /* BEGIN new.c */

    > >
    > > > #include<stdio.h>
    > > > #include<limits.h>

    > >
    > > > #define str(s) # s
    > > > #define xstr(s) str(s)

    > >
    > > > int main(void)
    > > > {
    > > > puts("/* BEGIN new.c output */\n");
    > > > if (-2147483648 > 0) {
    > > > puts("-2147483648 is greater than zero.\n");
    > > > }
    > > > if (0 > (-2147483647 - 1)) {
    > > > puts("(-2147483647 - 1) is less than zero.\n");
    > > > }
    > > > if (-2147483648 == 2147483648) {
    > > > puts("-2147483648 == 2147483648\n");
    > > > }
    > > > printf("(long unsigned)-5 is %lu\n",
    > > > (long unsigned)-5);
    > > > printf("(long unsigned)-2147483648 is %lu\n\n",
    > > > (long unsigned)-2147483648);
    > > > printf("LONG_MAX is %ld\n", LONG_MAX);
    > > > printf("UINT_MAX is %u \n", UINT_MAX);
    > > > puts("INT_MIN expands to " xstr(INT_MIN));
    > > > puts("\n/* END new.c output */");
    > > > return 0;
    > > > }

    > >
    > > > /* END new.c */

    > >
    > > > --
    > > > pete

    > >
    > > --

    >
    > But does it mean "converting a negative value to an unsigned integer
    > type " is implementation specific ?


    Yes. (1 + UINT_MAX) is implementation defined.

    > does c99 standard say anything about this ?


    N869
    6.3.1.3 Signed and unsigned integers
    [#1] When a value with integer type is converted to another
    integer type other than _Bool, if the value can be
    represented by the new type, it is unchanged.
    [#2] Otherwise, 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.
    [#3] Otherwise, the new type is signed and the value cannot
    be represented in it; the result is implementation-defined.

    --
    pete
     
    pete, Jul 30, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aspa
    Replies:
    9
    Views:
    11,932
    Roedy Green
    Oct 29, 2003
  2. Ken Tough
    Replies:
    4
    Views:
    4,125
    Ralmin
    Jun 8, 2004
  3. NM
    Replies:
    6
    Views:
    469
    Default User
    Sep 20, 2006
  4. Olaf
    Replies:
    4
    Views:
    3,173
    Mike Treseler
    Jun 11, 2007
  5. pozz
    Replies:
    12
    Views:
    743
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page