How we can create and use global and local variables in CPP? Discuss the conflict between float, double and long double data type decimal literals in CPP,

Practice # 6


#include <iostream>
using namespace std;
int c=30; // this is a globel veriable 
          // because it is created outside
          //of the local function
int main(){
int a,b,c; // thid is also a method to define veriables
cout<<"Enter the value of first number"<<endl;
cin>>a;
cout<<"Enter the value of second number"<<endl;
cin>>b;
c=a*b;
cout<<"Multiplication of 1st and 2nd number is : "<<c<<endl;
/*Here is we use scope resolution operator '::' to define same veriable
value of 'c'in local function. If we did'nt use scope resolution
operator we will get the same value of 'c' veriable as defined in
the local function because the preference will go the local veriable. */
cout<<"This is Global Veriable value : "<<::c<<endl;
//int d=(c+c);
cout<<"The Sum is Global and local veriable is : "<<::c+c<<endl;
/*Next we see below the by-defult conflict between float, double and long double
data type. If we define decimal litral like 25.45, or any other values for
both float and long double, the cpp compiler by-defult take that decimal litral
as double. So how we can define float data type value even we have use long-
double? The solution is, we will specifies our decimal value along a specific
alphabatic cheracter or suffix like this "25.45f" for float and "25.45l" for
long double. It is does'nt matter the alphabatic charecter is capital or small.*/
float x=25.45f;
long double y=25.45l;
//Here we prove that with help of keyword "Sizeof()".
cout<<"The size of 25.45 is : "<<sizeof(25.45)<<endl;
cout<<"The size of 25.45f is : "<<sizeof(x)<<endl;
cout<<"The size of 25.45l is : "<<sizeof(y)<<endl;
return 0;
}

Comments