Dumb char problem - Programmer

This is a discussion on Dumb char problem - Programmer ; I'm having difficult understanding what's going on here let me explain my quandry. char x; x = 133 % 256; Now can someone please tell me the value of x? This is not a trick question, but he's what happens ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Dumb char problem

  1. Dumb char problem

    I'm having difficult understanding what's going on here let me explain my
    quandry.

    char x;
    x = 133 % 256;

    Now can someone please tell me the value of x?

    This is not a trick question, but he's what happens when I print it out this
    way.

    printf("%x %x %d", x, 133 % 256, 133 % 256);

    This prints out "FFFFFF85 85 133"

    Why does it have to be 0xFFFFFF85, because it's really screwing things up!
    Can someone please explain as I want it as 0x85.

    Thanks,

    Martin



  2. Re: Dumb char problem

    Because %x is looking for an int and not a char?

    --
    -------------------------------------------------------------------------
    Free software - Baxter Codeworks www.baxcode.com
    -------------------------------------------------------------------------


    "Martin John Brindle" wrote in message
    news:aWgfc.304$74.2@newsfe1-win...
    > I'm having difficult understanding what's going on here let me explain my
    > quandry.
    >
    > char x;
    > x = 133 % 256;
    >
    > Now can someone please tell me the value of x?
    >
    > This is not a trick question, but he's what happens when I print it out

    this
    > way.
    >
    > printf("%x %x %d", x, 133 % 256, 133 % 256);
    >
    > This prints out "FFFFFF85 85 133"
    >
    > Why does it have to be 0xFFFFFF85, because it's really screwing things up!
    > Can someone please explain as I want it as 0x85.
    >
    > Thanks,
    >
    > Martin
    >
    >




  3. Re: Dumb char problem

    In article ,
    "Martin John Brindle" wrote:

    > printf("%x %x %d", x, 133 % 256, 133 % 256);

    ^ x gets sign-extended to an int here. Should be:

    printf("%x %x %d", (unsigned) x, 133 % 256, 133 % 256);

    Or:

    If you had declared x as:

    unsigned char x;

    then the compiler would have widened it to an int appropriately.

+ Reply to Thread