Thread: Variable Help

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    Variable Help

    Okay this is noob question, but i am a noob so i guess it makes sense.

    I am teaching myself C++ and i have two questions about the char data type

    1. I know the char has the same value range as a unsigned char or signed char (depending on the compiler) but i was wondering if there was any other differences between the char variable and the unsigned/signed char variable.

    2. Can you store the whole numbers in a char variable?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    The way I understand it is this

    char holds i believe it is values from -127 to 127 (or something similar
    unsigned char holds from 0 - 255

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    1) Plain char is either signed or unsigned depending on the implementation. The size of a char is always the same whether signed or unsigned and must be at least 8 bits wide, the only difference is that a signed char allows negative values and thus cuts the positive range approximately in half.

    For example, if a char is 8 bits wide you can safely assume that signed char has a range of -127 to 127 and unsigned char has a range of 0 to 255 as Vicious described above. Binary representations can affect the range. Using two's complement the negative range is increased by one for example.

    One subtle but important difference between signed and unsigned char is that signed char can have trap representations while unsigned char cannot. This tidbit of information is not terribly important until much later when you will need to understand why certain things must be done in a special way such as the cast for toupper in the following code.
    Code:
    #include <cctype>
    #include <iostream>
    
    int main()
    {
      int ch;
    
      while ((ch = std::cin.get()) != EOF) {
        ch = toupper(static_cast<unsigned char>(ch));
        std::cout.put(ch);
      }
    
      return 0;
    }
    2) I am not quite sure I understand your question. char is simply an integer that is treated specially in many cases. This does not change the fact that you can still use it as an integer if you want. Though judicious type casting would be required in some cases where the standard library is not aware that you do not want the character representation of an integral value stored in a char variable.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    Thank you, You anwsered my question perfectly.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Well, char and unsigned char variables get promoted to int type under certain circumstances (like when used as argument to a function, or when assigned to an int variable, or when used in mathematical expressions mixed in with int thingies).

    When getting promoted to int, the difference is that unsigned char has the upper bits padded with zeros, whereas char has the upper bits padded with the value of the most significant bit. There is a very good reason for this (if you intend to use them as numbers.)

    Try this, and make sure you understand what's happening:

    Code:
    #include<iostream>
    int main()
    {
      char char_variable;
      unsigned unsigned_char_variable;
    
      int int1;
      int int2;
      int whole_number;
    
    
      whole_number = 143;
    
      char_variable = whole_number;
      unsigned_char_variable = whole_number;
    
      int1 = char_variable;
      int2 = unsigned_char_variable;
    
      std::cout << "whole_number = " << whole_number 
                << std:: endl;
      
      std::cout << "  Check it out:" 
                << std::endl;
      std::cout << "  int1 = " << int1 
                << ", int2 = " << int2 
                << std::endl
                << std::endl;
    
      std::cout << "  Hey, what happened here? Well, show the values in hex:" 
                << std::endl;
    
      std::cout << std::hex;
    
      std::cout << "  whole_number = " << whole_number << "(hex)"
                << std::endl;
      std::cout << "  int1 = " << int1 << "(hex)"
                << ", int2 = " << int2 << "(hex)"
                << std::endl;
    
      return 0;
    }
    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM