Char to Int Conversion ?

Discussion in 'C' started by techinspiration, May 27, 2010.

  1. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I was just playing with the code and find something amusing. Can someone tell me what is happening here.

    for ch=127, the program converts it into int safely and print it but for ch=128, it prints -128...for ch=129,it prints -127 and so on.
    for ch=255 it prints -1 and for 256 it prints "warning: overflow in implicit constant conversion"

    I can understand the warning as we are only dealing with ASCII chars and they value upto 255 but why sign and value change at 128 ?

    And ya what is the proper way of converting a char to int ?
    Thanks.

    Code:
    #include<stdio.h>
    main()
    {
    
            char ch=128;
            int i;
            i=(int)ch;
            printf("%d",i);
    }
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    Code:
    #include<stdio.h>
    
    int main()
    {
        int i;
        [B][I]unsigned [/I][/B]char ch = 129;
    
        i = (int) ch;
        printf("%d\n", i);
        return 0;
    }
     
  3. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    As indicated, your char is signed (and for that reason can only contain values between -128 and +127).


    Demonstration for 3-bit numbers
    Draw a circle on a piece of paper. Put marks at 45, 90, 135 etc degrees.

    Number the top mark 0 (12 o'clock) and number the other marks consecutively clockwise. The last number will be 7. Write the binary value next to these numbers.
    Do the same with negative numbers (on the same circle) counting counterclockwise (0 at 12 o'clock). You can stop at -4 (6 o'clock).

    This shows that an unsigned 3-bit number goes from 0 to 7 and a signed one from -4 to +3.
    The most signficant bit indicates the sign
    Code:
    000 0 +0
    001 1 +1
    010 2 +2
    011 3 +3
    100 4 -4
    101 5 -3
    110 6 -2
    111 7 -1
    ou can draw the circle yourself for 4-bit (divide circle in 16 instead of 8; ranges 0 to 15 and -8 to +7) and for chars, integers, long integers etc :D

    Hope that this did helped a bit and did not cause more confusion.
     
  4. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    for char the valid range is -128 to + 127 (-2 ^ 7 to + 2^7 -1)
    when u goe up from this range .it come back at first valid character.
    so when u check for 128 then it goes out from the valid range 127.
    so next no is -128...it will print it.
     
  5. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    in a two's complement system

    the range of unsigned char between 0 to 255 < no negative number>
    the rannge of signed from -128 to 127

    unsigned binary signed
    127 01111111 127
    128 10000000 - 128
    129 10000001 - 127
    ..
    255 11111111 0
    <and the loop continue from the beginning>
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice