CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    6

    Casting variables

    I'm learning about casting variables, but something has come up very odd and I can't figure it out!

    Please examine this piece of code and explain to me why my result comes out as 65.

    Code:
    int num = 7;
    char letter = 'A';
    
    num = static_cast <int> (letter);
      cout<<"Cast character int: " << num << endl;
    When printed out, num is equal to 65 however I see no mathematical equation that could have changed num's value unless it has to do with the casting. Please explain this for me, I can't figure it out no matter how hard I think.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Casting variables

    The value of 'A' is 65, as this is the case in ASCII, which is presumably in use on your system (and mine ).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Casting variables

    All characters are stored as numbers internally, 65 is for A(66 for B and so on). The casting only changes CHAR to INT which is 1 byte to 4 byte, which does not change anything.
    Regards,
    Ramkrishna Pawar

  4. #4
    Join Date
    Dec 2009
    Posts
    6

    Re: Casting variables

    Oh ok thank you both of you. My first thoughts were it had something to do with the ASCI or ASCII table but I wasn't sure. Hehe I'm learning more each day.

  5. #5
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Casting variables

    Well, the character mapping can be seen in table, but OS knows them only by numbers, when it comes to display the showing characters thing happen by drawing based on the font template, the appropriate shape is drawn on screen based on it's number.
    Regards,
    Ramkrishna Pawar

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured