casting
- From: Christian Christmann <plfriko@xxxxxxxx>
- Date: Fri, 09 Jun 2006 10:09:25 -0600
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
.
- Follow-Ups:
- Re: casting
- From: CBFalconer
- Re: casting
- From: Keith Thompson
- Re: casting
- From: Richard Heathfield
- Re: casting
- From: Dann Corbit
- Re: casting
- Prev by Date: Re: Is "?" a sequence point?
- Next by Date: Structure of functions
- Previous by thread: Quite Smart RIddle
- Next by thread: Re: casting
- Index(es):
Relevant Pages
|