casting



Hi,

a question on castings.

My code example:

#include <stdio.h>

int main( void )
{
unsigned int a = 4294967295U;
signed int b = 4294967295U;
signed int c = (signed int) a;

printf( "a:%ud\n", a );
printf( "b:%ud\n", b );
printf( "c:%ud\n", c );

return 0;
}



The output is:
a:4294967295d
b:4294967295d
c:4294967295d

I don't understand why "b" and "c" are also a 32-bit values. Since I
defined "b" and "c" as signed int, there are ony 31 bits that can be used
to represent the number, thus the value range is [-2147483648, 2147483647].
When assigning "b" the value 4294967295U, I thought that an implicit cast
is performed that converts the value to a 31bit value + 1 bit for the sign.
In a similar way, "c = (signed int)a" is an explicit cast that should
convert the 32bit value of "a" into a 31bit value represented by "c".
However, printf indicates that casting is not performed. Why?

Regards,
Chris
.



Relevant Pages

  • Re: casting
    ... a question on castings. ... unsigned int a = 4294967295U; ... signed int c = a; ... When assigning "b" the value 4294967295U, I thought that an implicit cast ...
    (comp.lang.c)
  • Re: casting
    ... Christian Christmann said: ... a question on castings. ... signed int c = a; ... A cast is an explicit conversion. ...
    (comp.lang.c)
  • Re: C standard question?
    ... uc1 gets promoted to a signed int ... On many implementations, perhaps including all those you have ever ... uc1 gets promoted to signed int. ... Of course, on implementations where uc1 is promoted to unsigned int, ...
    (comp.lang.c)
  • Re: GCC bitfield packing
    ... The actual amount of storage taken us depends only upon the number of ... depend upon the type of the bitfield declaration. ... _Bool, int, signed int, and unsigned int. ...
    (comp.std.c)
  • Re: Promotion and assignment
    ... Tomás Ó hÉilidhe wrote: ... signed int or unsigned int ... that the lvalue E1 is evaluated only once." ...
    (comp.lang.c)