Thread: How to know whether char is signed or unsigned on your system?

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    How to know whether char is signed or unsigned on your system?

    Is there any special function built into the c++ library that accomplishes this?

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    Is there perhaps another way? I just recently started learning about c++, though I do have a background in java.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    First and foremost, what are you trying to solve? Also, keep in mind, C++ has a char and an unsigned char. So unless you have a wonky implementation, you can assume that char is signed.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    I got help from someone. So we are basically setting char variable x with 255. Then if x is less than 0 then char is unsigned, signed otherwise. Can you explain what is happening when char x = 255? Or is it as simple as storing 255 inside x?


    Code:
        char x = 0xFF;
    
    
        if (x < 0) {
            printf("char is unsigned\n");
        } else if (x >= 0){
            printf("char is signed\n");
        }
    Last edited by asilvester635; 01-27-2017 at 06:09 PM.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by MutantJohn View Post
    First and foremost, what are you trying to solve? Also, keep in mind, C++ has a char and an unsigned char. So unless you have a wonky implementation, you can assume that char is signed.
    Although a separate type, char's representation is equivalent to either signed char or unsigned char, but which one is implementation defined.

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    Sorry, there was a mistake in my code. Her is the correct (I think) code.

    Code:
        char x = 0xFF;
    
    
        if (x < 0) {
        	printf("char is signed\n");
        } else if (x >= 0){
        	printf("char is unsigned\n");
        }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char x = 0xFF;
    This presumes that chars are 8-bit numbers, which isn't universally true.

    The GCC compiler allows you to choose whether the basic 'char' type is signed or unsigned.
    -funsigned-char
    -fsigned-char


    is_signed (see post #2) seems the best idea. Anything else is looking hacky if it's going to be anywhere near portable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Salem is correct. A char is a byte but a byte isn't guaranteed to be 8 bits.

    is_signed is the best approach. It requires more advanced C++ but it's the only true solution. Keep in mind, signed types are also not universally represented. There are myriad ways of laying out the representation of a signed integral type. Crude assignment like that will be unreliable and error prone.

    Use is_signed.

    Code:
    static_assert(std::is_signed<char>::value);

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MutantJohn
    Keep in mind, signed types are also not universally represented. There are myriad ways of laying out the representation of a signed integral type.
    However, for the signed integer representation the standard only permits sign and magnitude, one's complement, and two's complement representations.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. Signed/Unsigned char issues
    By NewbGuy in forum C Programming
    Replies: 14
    Last Post: 07-11-2009, 02:27 PM
  3. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  4. unsigned and signed char
    By P.Phant in forum C Programming
    Replies: 3
    Last Post: 07-12-2003, 05:00 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM

Tags for this Thread