Re: Type conversion
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Thu, 31 Jan 2008 15:20:30 +0000
manik <manik.budhiraja@xxxxxxxxx> writes:
Assuming int is 32 bits and char is 8 bits on my machine
Say I have the following piece of code
int a = 0x12345678;
char b;
b=(char)a;
b = a;
is simpler and has the same effect. Casts are for cases where type
conversion do not otherwise take place.
What would be the value of b?
Even the value of a in doubt! Let's assume that the initialisation of
a does not overflow...
Would the value of b depend on the endianness of the machine?
No, not in any usual sense of the word.[1]
If char is unsigned the value is guaranteed by the standard (it is the
value of a mod UCHAR_MAX + 1) but the unsigned-ness of char is not
guaranteed, so the code is not portable. If char is signed, and the
value of a does not fit (it might!) then the result is implementation
defined.
[1] I say this because (since the result may be implementation-defined)
the implementation is permitted to cause the value of b to depend on
the (settable) endian-ness of the machine, but that is not what one
usually means by the phrase.
--
Ben.
.
- References:
- Type conversion
- From: manik
- Type conversion
- Prev by Date: Re: declaration of variable in for loop
- Next by Date: Re: Getting user input in Linux?
- Previous by thread: Re: Type conversion
- Next by thread: I women looking for a man the usual sex Haley
- Index(es):
Relevant Pages
|