conversion of longer int to shorter ones

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jan 12, 2007.

  1. Guest

    Hi,

    Consider following piece of code:

    int i = 0x12345678;
    char c;
    c = i;
    printf("0x%x\n",c);

    What value will be printed ?
    As per K&R, longer integers are converted to shorter ones by dropping
    the higher order bits. So, value printed should be "0x78".

    However, my question is that can this value be different on machines
    with different endianness ?

    thanks a lot for any help ...
     
    , Jan 12, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > Consider following piece of code:
    >
    > int i = 0x12345678;
    > char c;
    > c = i;
    > printf("0x%x\n",c);
    >
    > What value will be printed ?
    > As per K&R, longer integers are converted to shorter ones by dropping
    > the higher order bits. So, value printed should be "0x78".


    In standard C, this is only true for unsigned integer types. On many
    implementations this is also true for signed integer types, but if
    possible, don't rely on it.

    > However, my question is that can this value be different on machines
    > with different endianness ?


    It is not likely to depend on endianness. If both types are declared as
    unsigned, and if the size of the types is what you expect they are
    (you're assuming an int is large enough to store 0x12345678, and that
    char only has 8 bits), then it would print 0x78 no matter how the value
    is stored in memory. However, with signed types, "either the result is
    implementation-defined or an implementation-defined signal is raised".
    The output could be 0x78. But it's just as possible to have 0xFF
    assigned to c, or 0, or to have the program abort.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jan 12, 2007
    #2
    1. Advertising

  3. CBFalconer Guest

    "" wrote:
    >
    > Consider following piece of code:
    >
    > int i = 0x12345678;
    > char c;
    > c = i;
    > printf("0x%x\n",c);
    >
    > What value will be printed ?
    > As per K&R, longer integers are converted to shorter ones by dropping
    > the higher order bits. So, value printed should be "0x78".


    Provided that CHAR_BIT is 8.

    >
    > However, my question is that can this value be different on machines
    > with different endianness ?


    No.

    --
    "I have a creative mind. You (singular) are eccentric.
    He is insane. We are losing sight of reality.
    You (plural) are smoking crack. They are certifiable."
    Declension of verbs, per Lewin Edwards
     
    CBFalconer, Jan 12, 2007
    #3
    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. Richard
    Replies:
    2
    Views:
    381
    Jacqui or (maybe) Pete
    Jul 10, 2003
  2. Schnoffos
    Replies:
    2
    Views:
    1,237
    Martien Verbruggen
    Jun 27, 2003
  3. Mik0b0

    Longer array out of two shorter arrays

    Mik0b0, Jan 13, 2007, in forum: C Programming
    Replies:
    15
    Views:
    424
    Keith Thompson
    Jan 13, 2007
  4. trvlDev
    Replies:
    3
    Views:
    268
    trvlDev
    Apr 6, 2007
  5. Steventangle
    Replies:
    4
    Views:
    106
    StevenTangle
    Mar 23, 2005
Loading...

Share This Page