Output writing implicit typecasting – Computer Sir Ki Class
  

Login
[lwa]



Solved Problem! #CPP#3208 siteicon   siteicon   siteicon  

Problem Statement - Output writing implicit typecasting

Write output of the following program if the input given is R

TC++ #3208

#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
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;
getch();
return 0;
}
#include <iostream>
using namespace std;
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;
}

Test it !

Solution


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.


Share

sunmitra| Created: 30-Jan-2018 | Updated: 30-Jan-2018|






Back