-
December 28th, 2009, 04:45 AM
#1
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.
-
December 28th, 2009, 04:48 AM
#2
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 ).
-
December 28th, 2009, 04:54 AM
#3
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
-
December 28th, 2009, 04:59 AM
#4
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.
-
December 28th, 2009, 05:22 AM
#5
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
-
December 28th, 2009, 10:17 AM
#6
Re: Casting variables
char to int is a standard integral promotion, so you didn't need the static_cast there.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|