-
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.
-
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 :) ).
-
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.
-
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.
-
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.
-
Re: Casting variables
char to int is a standard integral promotion, so you didn't need the static_cast there.