Thread: Signed/Unsigned Chars

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    Signed/Unsigned Chars

    Here is one line from C programming language book:

    "Whether plain chars are signed or unsigned is machine-dependent, but printable characters are always positive."

    My doubt is first of all What does this line means..because suppose my machine has signed chars..How i can i print a character with ASCII value 234 for example because range for signed chars is -128 to +127.

    How can compiler interpret 234 correctly and print it as a character in a right way even when it doesn't come under the range.??

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The reason it works is that there's only 127 ascii characters.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    But C also support extended ASCII character set.

    It works when i write

    char c = 234;
    printf("%c",c);

    even when my machine takes above declaration of char as signed.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yeah, but if you assign for example -2 to a unsigned char it will come out as 254.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    Yeah that's wright. but when i write char c = 234 shouldn't it print the character with ASCII value -22 (which is invalid) on my machine but it is printing the character right character.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    -22 maps to 234 as unsigned. Trying this "printf("%c\n", -150);" for example will print 'j'.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use an unsigned char. Basically it says that all values > 0 is printable. That -22 maps to 234 is just because two-complement works that way. Don't depend on it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    But that means you should use unsigned chars for extended ascii if char is signed by default, since assigning values larger than 127, does depend on this.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You shouldn't be using extended ASCII anyway. If you need to do that, you should be using unicode.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well, then what's the point of using or caring if a char is signed or not in the context of ascii since all legal values fall with in the positive range of a signed char.

    My point was that if you should not depend on the fact that a negative number corresponds to the positive value +127 (not taking into account eventual overflow), then using an unsigned type is not optional.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by ankitsinghal_89 View Post
    How can compiler interpret 234 correctly and print it as a character in a right way even when it doesn't come under the range.??
    The answer lies in the very quote you posted, in red below.
    Code:
    "Whether plain chars are signed or unsigned is machine-dependent, but printable characters are always positive."

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Subsonics View Post
    Well, then what's the point of using or caring if a char is signed or not in the context of ascii since all legal values fall with in the positive range of a signed char.

    My point was that if you should not depend on the fact that a negative number corresponds to the positive value +127 (not taking into account eventual overflow), then using an unsigned type is not optional.
    Absolutely. But char is also used to hold raw data, and for this purpose, unsigned is usually a better choice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ankitsinghal_89 View Post
    How can compiler interpret 234 correctly and print it as a character in a right way even when it doesn't come under the range.??
    More specifically, since you may not understand this part and no one has bothered to explain it: in "two's compliment", which is how most common systems store signed values (but not all, and it is not part of the C standard, so as Elysia says, you should not count on this, altho it is probably true on your system) the signed value -1 (for example) has the exact same binary representation as the unsigned value 256. The binary representation of 234 (which could only be unsigned) is exactly the same as the binary representation of the signed char value -22. So if you assign 234 to a signed char and print out it's signed value, it's -22.

    http://en.wikipedia.org/wiki/Two%27s_complement
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    Thanks,i understood it now.

    Can you guys explain this also.

    If i do something like

    int i;
    char c;

    i = c;

    In this case can information be lost ever??I think it information can be lost because it depends on your m/c if it uses sign extension or not while promoting char to int.

    What do your guys think??

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ankitsinghal_89 View Post
    In this case can information be lost ever??I think it information can be lost because it depends on your m/c if it uses sign extension or not while promoting char to int.
    If they differ in signedness, yes. But if they are both signed, an int assigned to the value of a char will always be the same number (-1, 123, etc). The compiler takes care of that (pretty sure the C standard insures this, but I have never checked).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filter invalid chars such as 007F,...
    By securelinprog in forum C Programming
    Replies: 14
    Last Post: 04-07-2010, 11:29 PM
  2. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  3. Replies: 2
    Last Post: 01-06-2009, 10:37 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM