Example(s):Palindrome, prime, armstrong, "linear search", reverse etc.
Example(s):1575, 1632, 1539 (Only one at a time)
Login
[lwa]
Solved Problem
#CPP#3208
Problem Statement - Output writing implicit typecasting
Write output of the following program if the input given is R
int main()
{
char x, y=32;
cout<<"Input an uppercase alphabet: ";
cin>>x;
char c=x+y;
int d=x+y;
cout<<"Output 1 would be "<<c<<endl;
cout<<"Output 2 would be "<<d<<endl;
return 0;
}
Solution
TC++ #3208
Run Output
Input an uppercase alphabet: R
Output 1 would be r
Output 2 would be 114
char x, y=32;
Both x and y are character type variables. 32 is stored in y. Although 32 is a number but characters store only ascii numbers against characters. Char storage is nothing but a place to store 8 bit numeric value.
cout<<“Input an uppercase alphabet: “; cin>>x;
Here we collect the value of x. If R is input what actually would get stored would be 82
char c=x+y;
Here x as 82 and y as 32 would be stored making it 114 as stored value but type as char so if c is displayed it would be small r (ascii 114)
int d=x+y;
here storage 114 is done as integer, so if displayed it would be displayed as 114
cout<<“Output 1 would be “<<c<<endl; cout<<“Output 2 would be “<<d<<endl;
Will display small r (the char form of ascii 114) and 114 the integer form directly.
Notes
The display of char and int forms in 8 bit form are directly interchangeable as the storage is practically the binary number form only. char display or num display are just way to represent them on screen. This kind of conversion is called implicit conversion.