EmbLogic's Blog

Negative characters in c

??how char can be signed / unsigned ???

how can we define a negative char ….

and why we need negative char ?

2 Responses to Negative characters in c

  1. msiddarth says:

    The three types char, signed char, and unsigned char are collectively called the character types.
    Compilers have the latitude to define char to have the same range, representation, and behavior as either signed char or unsigned char.
    Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.

    Use only signed char and unsigned char types for the storage and use of numeric values, as this will take only one byte as comparison to integer which takes 4 bytes to store a numeric value. However, the restriction with respect to char data types is that it can take only upto 255 values only.

  2. kamran ali says:

    just to add in the above answer:the char holds 8bit in the memory.out of which the MSD is used as a flag.hence the break up of 8 bit is: 1 bit for signed and 7 bit for data bit.

    example: char a=7;
    7(dec)=111(binary)
    so the 7 data bit are saved as: 0000111
    and the 8 bit is 0 ( since it is signed).

    Now for the next example:
    char a=-7;
    7(dec)=111(binary)
    so the bits are 00000111.But here is the catch, since the number is negative the complier save it as 2′s compliment.

    therefore,
    1′s compliment: 11111000
    adding 1 to it will give the 2′s compliment
    2′s compliment : 11111001

    the MSD give info to the complier that the input is a negative number:

    hope it will help.

    As far as the need of negative char,
    it depends on the programmer, but sometimes it is needed or in other words you are compelled to use it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>