In ASCII character 2 has code 50. So 50 – 47 will result in 3.
Thus if in statement
cin>>s;
you enetered 2
then in statement
cout<<(*s.begin())-47;
expression *s.begin()-47
that is equivalent to '2' - 47
is converted to type int
due to the integer promotion and is equal to 3 ( ‘2’ – 47 => 50 – 47 == 3).
Take into account that call s.begin()
returns iterator that points to the first character of the string and *s.begin()
yields the character itself.
solved Explain the output when a integer is given as input [closed]