Reference Variables & Typecasting in C++
#include<iostream>
using namespace std;
int c = 45; //Global Variable
int main()
{
// ****************Built in data types******************
// int a,b,c;
// cout<<"Enter the value of a : "<<endl;
// cin>>a;
// cout<<"Enter the value of b : "<<endl;
// cin>>b;
// c = a+b;
// cout<<"The sum is : "<<c<<endl;
// cout<<"The global c is : "<<::c; //Global Variable
// *****************float,double and long double literals*****************
// float d=34.4;
// long double e = 34.4;
//By default, "34.4" will be considered as double, not float
//But "34.4f" will be considered as float
//"34.4l" will be considered as long double
// cout<<"The size of 34.4 is "<<sizeof(34.4)<<endl;
// cout<<"The size of 34.4f is "<<sizeof(34.4f)<<endl;
// cout<<"The size of 34.4F is "<<sizeof(34.4F)<<endl;
// cout<<"The size of 34.4l is "<<sizeof(34.4l)<<endl;
// cout<<"The size of 34.4L is "<<sizeof(34.4L)<<endl;
// cout<<"The value of d is "<<d<<endl<<"The value of e is "<<e;
// ******************Reference variables*******************
// float x = 455;
// float &y = x;
// cout<<x<<endl;
// cout<<y<<endl;
// *******************Typecasting********************
int a=45;
float b = 45.46;
cout<<"The value of a is : "<<(float)a<<endl; //Both are same
cout<<"The value of a is : "<<float(a)<<endl;
cout<<"The value of b is : "<<(int)b<<endl;
cout<<"The value of b is : "<<int(b)<<endl;
int c = int(b);
cout<<"The expression is "<<a + b<<endl;
cout<<"The expression is "<<a + int(b)<<endl;
cout<<"The expression is "<<a + (int)b<<endl;
return 0;
}
OUTPUT:
> cd "d:\C++\" ; if ($?) { g++ tut7.cpp -o tut7 } ; if ($?) { .\tut7 }
The value of a is : 45
The value of a is : 45
The value of b is : 45
The value of b is : 45
The expression is 90.46
The expression is 90
The expression is 90
PS D:\C++>
Comments
Post a Comment