DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008
    Posts
    1

    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
    Location
    UK
    Posts
    604
    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.

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

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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:
    Code:
    char s = 1000; //truncation 
    cout <<(int) s<<endl; //you won't see 1000 here!
    cout<<1000<<endl;
    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

Bookmarks

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links