Re: Type conversion



santosh wrote:
manik wrote:

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;

What would be the value of b?

Would the value of b depend on the endianness of the machine?

Yes.

No; endianness doesn't affect the outcome.

Also note that for maximum portability use either signed or
unsigned char for holding non-character values. The signedness of plain
char is implementation dependant, thus creating needless
non-portability.

Right. If char is unsigned (and eight bits wide, by
assumption), the value stored in `b' will be 0x78 == 120.
If char is signed, the conversion to char produces an
implementation-defined result or raises an implementation-
defined signal ("trap").

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: why display is ffffff82 instead of 82 ??
    ... > but after i use the following commands, the last byte 82 is displayed as ... char is a signed integer type. ... Casting to int retains this ... signedness and extends the sign-bit into all available "negativity" ...
    (comp.lang.cpp)
  • Re: Confusion about integer promotions.
    ... > "An rvalue of type char, signed char, unsigned char, short int, or ... > unsigned short int can be converted to an rvalue of type int if int can ... where signedness should also be taken into ...
    (microsoft.public.vc.language)
  • Re: Type conversion
    ... manik wrote: ... int a = 0x12345678; ... unsigned char for holding non-character values. ... The signedness of plain ...
    (comp.lang.c)
  • char - signed or unsigned ?
    ... Is a variable of type "char" without any qualifier supposed ... Or is it implementation dependant? ... comparison is always true due to limited range ... The warning goes away if the user changes the declaration to: ...
    (comp.lang.c)
  • Re: char - signed or unsigned ?
    ... Or is it implementation dependant? ... char with that user's particular implementation is implemented as ... > Clarification of this issue will be appreciated. ...
    (comp.lang.c)