I am learning about implicit conversions in C++. And i read the following example:
char a;
std::cin>>a; //I can enter an integer like 56 here
std::cout<<a<<std::endl; //for the input 56 it will display 5 because of its ASCII value
I understood the above example by reading about it in different books and posts on SO. For example, if i provide the input J
, then the program successfully prints J
on the console. Similarly if i provide the input say 56
then the output will be 5
because of its ASCII value.
But then i tried the opposite as shown below:
int a;
std::cin>>a;//if i provide the input as the character J then why is the output 0 instead of the corresponding code point of `J`
std::cout<<a<<std::endl;
For the above snippet, if i provide the input 56
then the output is correctly printed as 56
. But if i provide the input as J
then the output is 0
.
So my question is in the above 2nd snippet why the code point corresponding to the character J
is not printed and instead we get 0
printed on the console. I mean, a
is an integer variable so it is able to store the code point corresponding to the character J
and then when we do cout<<a;
we should be getting that code point as the output instead of 0
. What is happening here. Is this related to implicit conversion like a char
can be promoted to an int
or something else.