Under what condition can an int variable be assigned a char? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Under what condition can an int variable be assigned a char?

#include <iostream> using namespace std; int main() { int num = (int)'A'; char ch = num; cout << ch; return 0; } Why does this output A? 'A' alone is not an integer.. Does the (int) convert 'A' into an integer data type?

5th Aug 2020, 9:29 AM
Solus
Solus - avatar
3 Answers
+ 6
here's quick hack on how to remember basic ascii values.. A = 65 B = A + 1 = 65 + 1 = 66 C = A + 2 = 65 + 2 = 67 or C = B + 2 = 66 + 1 = 67 . . . Z = A + (distance b/w Z and A) = 65 + (26 - 1) = 65 + 25 = 90 One more example: Y = A + (distance b/w Y and A) = 65 + (25 - 1) = 65 + 24 = 89 same applies for small letter a = 97 b = a + 1 = 97 + 1 = 98 . . z = a + (distance b/w z and a) = 97 + (26 - 1) = 97 + 25 = 122 same goes for numbers and symbols (with little effort)
5th Aug 2020, 10:02 AM
Rohit
+ 3
~ swim ~ Got it. Will do some memorizing on these 5 points RKK Thank you for all the examples.
5th Aug 2020, 10:18 AM
Solus
Solus - avatar
+ 1
~ swim ~ My 3 weeks experience of c++ is from the Sololearn course and practice problems only. No prior experience. How much of the ASCII table should be memorized? Okay I'll research a bit more on the relationship between chars and integers.
5th Aug 2020, 9:54 AM
Solus
Solus - avatar