Thread: Type Conversions

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Type Conversions

    What actual;y happens when one type is converted from another.

    For example,

    If I have an unsigned char which is one byte, and I wanted do display it as a number

    Code:
    std::cout << (int)char_var;
    does it actually convert it to an int and use 32-bits of memory?

    I guess my main question is; is it any more efficient to store a number under 255 in a char?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    in the case above it is converted to an int which means it will take up sizeof(int) bytes for the call to cout, once cout is completed the memory will be freed.

    As for efficienty it depends on what you are trying to be efficient with. Memory efficient when storing a lot of variables: yea, storing one variable: probably not (think byte alignment), speed efficient: it depends.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    It just seemed logically correct to store numbers under 255 in one byte instead of 4 bytes.

    Thank you Thantos.

  4. #4
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Well sometimes a char just has to be a char, and an int an int
    Shortly written
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    What actually happens when one type is converted from another.
    This is called Typecasting, or sometimes just casting. Like Thantos said, it does actually convert it. Leading-zero bits will be added when converting to a larger type, and higher-order bits are dropped when converting to a smaller type. When you convert between an integer-type and a floating-type, the storage format changes.

    [EDIT] - Well, the above isn't really true... or at least not clear... I shouldn't have used the word "converted" at all! Thantos' answer was better: It creates a temporary int for use by this particular statement, or simply "pretends" that char_var is an int for this particular statement. char_var does NOT get changed to an int! In fact, char_var itself isn't affected in any way... it is still a type-char after that line of code is executed!


    That cout "trick" simply takes advantage of the fact that cout is "smart" (overloaded?). For example, the C-language doesn't have cout, and you always have to specify if a variable is to be displayed as a number or an ASCII character. I believe there is a more direct way to make cout show the numerical value of a character, but I'd have to look it up.



    I guess my main question is; is it any more efficient to store a number under 255 in a char?
    Maybe. It is considered "good practice" not to hog memory, and it is also good practice not to assume a particular operating system / platform. But, my 32-bit system can't address anything less than 32 bits at a time.



    ...instead of 4 bytes.
    FYI - An int on your system may be 4 bytes, but the language standard symply says at least 2-butes.

    Here are the minimim capacities for each type:
    char -128 to +127 (1 byte)
    unsigned char 0 to 255 (1 byte)

    int -32,768 to +32,767 (2 bytes)
    unsigned int 0 to 65535 (2 bytes)

    long -2,147,483,648 to +2,147,438,647 (4 bytes)
    unsigned long 0 to 7,294,967,295 (4 bytes)

    float +/- 2.1E-38 to +/- 3.4 E38 (4 bytes)
    double +/- 2.2E-308 to +/- 1.8 E308 (8 bytes)
    Last edited by DougDbug; 08-23-2004 at 06:57 PM.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Thank you very much.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    If you are going to use char to store and manipulate integers, note the following from DougDbg
    char -128 to +127 (1 byte)
    unsigned char 0 to 255 (1 byte)
    When a char is converted to an int, the sign (most significant bit) is extended into the upper bits. So, for example, a char with a value greater than or equal to 0x80 becomes a negative int, and an unsigned char variable always becomes a positive int.

    Confused? (Probably not, for experienced programmers, but for some people, maybe. That's the price you pay for doing something above and beyond naive usage of int for arithmetic (since in C, all char and int --- and their unsigned counterparts --- arithmetic involving char and int is always done as integer arithmetic).

    Run the following if you have any questions.

    Code:
    #include <iostream>
    
    int main()
    {
      int i = 8;
      int j = 180;
      char c = 8;
      char d = 180;
      unsigned char e = 180;
    
      int k;
      int m;
    
      std::cout << "i = " << i << ", j = " << j << std::endl;
      std::cout << "(int)c = " << (int)c << ", (int)d = " << (int)d <<std::endl;
      std::cout << "(int)e = " << (int)e << std::endl;
    
      k = i + d;
      m = i + e;
    
      std::cout << std::endl << "k = " << k << ", m = " << m << std::endl;
    
    
      return 0;
    }
    Note that k is not equal to m.


    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM