Thread: signed char type

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    11

    signed char type

    Hi every body!

    I was reading about " char" data type. Here is what my book says:

    "If the character is of a signed category, you can declare it as a signed character. This type of variable would be an 8-bit integer whose value can range from –128 to +127. "

    But when i use the "signed char" in my program and test it by inputting " -1", the program only takes " first character which is "-" and ignores the " 1". It appears to me, only first character is accepted. There is no way i can enter "-1" as input 1.e

    signed char t;
    cin >> t

    Though the book says the value for "t" can range from -128 to 127 , but when i input "-1" , only " -" is placed in varaiable "t".

    Thanks a lot!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "-" is a character with a value (look it up on ASCII table to be sure). You can never input a char via value directly, but only by typing a character with the value you want. (If you want the char to hold 50, you would have to type the character with ASCII value 50 et cetera.)

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    11
    Thanks for your reply.
    Let say i declare the char data type as:

    signed char t;

    Now i want " t" to store " -50"

    How can i accomplish this? as " t" can only hold one character while my input is consists of three characters . Also my book says the range of values for signed char type is –128 to +127.

    Please excuse me for repeating my problem.

    thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is nothing you can type as input that will cause the computer to believe you have typed in a character with ASCII code -50.* Think for five seconds: when you read in a character and someone types "a", you expect the value of the character to be a, not "hey that's not a number!" You don't type ASCII values at the keyboard.

    You can do:
    Code:
    signed char t;
    t = -50;
    *I suppose you could type in character 206, which if I've done my math correctly would be represented in a signed char as -50.

    **I've been assuming ASCII all along, but it's probably true.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    11
    Thanks tabstop ! I got it now.

    Look likes the book is wrong in saying the signed char has a range -128-to 127. All characters are represented from 1 through 127 value on ASCII table. I don't see any negative value on ASCII table either.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sarahr202 View Post
    Thanks tabstop ! I got it now.

    Look likes the book is wrong in saying the signed char has a range -128-to 127. All characters are represented from 1 through 127 value on ASCII table. I don't see any negative value on ASCII table either.
    The book is absolutely correct. You can calculate with a signed char in that range. You can't read it in with %c. (You can probably read it in with %d.)

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can read extended characters in with %c if as long as the input is not raw user typing. GNOME terminal at least seems to disallow extended characters to be entered directly into the terminal, though you can pipe input from echo, that features extended characters.

    On windows, typing alt+character should give the extended version. In this case alt-shift-6 (assuming american keyboard) should give -50. This is untested, however.
    Last edited by King Mir; 05-20-2009 at 11:37 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by tabstop View Post
    The book is absolutely correct. You can calculate with a signed char in that range. You can't read it in with %c. (You can probably read it in with %d.)
    Not directly. I think what the OP is looking for:
    Code:
    int xa;
    signed char xb;
    
    std::cint >> xa;
    if(xa < std::numeric_limits<char>::min() || xa > std::numeric_limits<char>::max())
    {
        // Handle out of range.
    }
    else
    {
        xb = xa;
    }
    While this might be what you want, this is probably not what you need. The max/min above should really be replaced by whatever bounds you have in your problem. (If your input ranges from -100 to 100, then those are the numbers you should use.)
    Additionally, in today's age - what's wrong with an int? Unless you have special memory usage concerns... go with an int. (It may even be faster processor-wise than char, and, depending on your variables, it might take up the same space.)
    Last edited by Cactus_Hugger; 05-21-2009 at 05:07 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User
    Join Date
    May 2009
    Location
    Soon to be moving to Visalia, CA
    Posts
    8
    Quote Originally Posted by sarahr202 View Post
    Hi every body!

    I was reading about " char" data type. Here is what my book says:

    "If the character is of a signed category, you can declare it as a signed character. This type of variable would be an 8-bit integer whose value can range from –128 to +127. "

    But when i use the "signed char" in my program and test it by inputting " -1", the program only takes " first character which is "-" and ignores the " 1". It appears to me, only first character is accepted. There is no way i can enter "-1" as input 1.e

    signed char t;
    cin >> t

    Though the book says the value for "t" can range from -128 to 127 , but when i input "-1" , only " -" is placed in varaiable "t".

    Thanks a lot!
    If all you are wanting to do is to assign the value "-50" to "t" using a signed char variable then this will work just fine:

    Code:
    signed char t[3];
    cin>> t;
    For a variable of type "char" you can define how many digits it can hold by simply adding the [number of digits needed] in your variable declaration.

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Gallaton View Post
    If all you are wanting to do is to assign the value "-50" to "t" using a signed char variable then this will work just fine:

    Code:
    signed char t[3];
    cin>> t;
    For a variable of type "char" you can define how many digits it can hold by simply adding the [number of digits needed] in your variable declaration.
    No. Just... ...no.

    signed char t[3] is one of two things:
    1) Three signed char's, which will hold values from numeric_limits<signed char>::min() to numeric_limits<signed char>max(), generally -128 to 127. (Assuming that signed char is a two's complement 8-bit integer.)
    OR
    2) A string, holding 2 characters and a null terminator.

    Your code will get you the second, along with the possibility of a buffer overflow if I enter too much data. Additionally, even if that signed char array is holding {'4', '2', '\0'}, it's not going to be of much use until you convert it to an integer...
    Code:
    std::stringstream s;
    int x;
    s << t;
    s >> x;
    Which would've been the same (or perhaps worse) than:
    Code:
    int x;
    std::cin >> x;

    Just in case the OP doesn't yet have an answer...
    Code:
    signed char t;
    cin >> t
    cin interprets t, which is a char, to be a 'character', litterally. It'll take one character from the input, and store it in t.
    Say you enter "42" - cin >> t will read in the '4', and store the numeric equivalent of '4' in t. (And '4' != 4 - it'll be the ASCII value of '4', most likely.)
    If you don't like that functionality, you have to either write it yourself (don't), or use cin >> (some integer), ensure it's within range, like I did above, and store into char. But usually and int will do just fine.
    Last edited by Cactus_Hugger; 05-21-2009 at 07:16 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM