DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008

    why different value for same char?

    Can someone explain why this ( http://tinyurl.com/5qvo44 ) prints different values?

    1) The character is non-English.. and it does not fit in base ASCII range.
    2) There is overflow happening too

    What I basically want to understand is why assigning a char and casting it to int gives a different value than just casting a raw char directly.

  2. #2
    Join Date
    Jan 2005
    int and char have different sizes in terms of bytes on your architecture.
    For example (in many machines these days) char has 1 byte and int has 4.
    So implicitly casting Tau to a char will lose information. the value will be in the range -128 to 127.
    when converting to an int the value will be in the range from -2^31 to 2^31-1

    That's it.

    the real values depend on your architecture! Above is only an example
    Last edited by drkybelk; 09-01-2008 at 09:46 AM.
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd

  3. #3
    Join Date
    Nov 2003
    There's also another issue here: signed vs unsigned char. Without knowing which compiler and OS are being used, you can't tell whether plain char is signed or unsigned.

    More specifically: the literal 'ע' (assume it's the Greek letter tau) stands for the numeric constant that is reserved for that letter. It may be larger than 8 bits. By contrast, initializing a char variable with 'ע' enforces truncation to 8-bits. Once the original numeric value has been truncated to 8 bits, you cannot expand it to its original value, so the cout expression that seeminly prints int merely prints the truncated value. Perhaps an example will illustrate this:
    char s = 1000; //truncation 
    cout <<(int) s<<endl; //you won't see 1000 here!
    Last edited by Danny; 09-01-2008 at 04:37 PM.
    Danny Kalev

Similar Threads

  1. Runtime error w/ Variable Type Part II
    By jamestmfbong in forum C++
    Replies: 2
    Last Post: 05-04-2009, 06:03 PM
  2. Gradebook program
    By [gx]Shadow in forum Java
    Replies: 5
    Last Post: 10-25-2006, 10:20 PM
  3. Replies: 9
    Last Post: 07-28-2005, 08:40 PM
  4. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 2
    Last Post: 09-01-2000, 11:36 AM
  5. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 0
    Last Post: 09-01-2000, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.