TOPIC: Understanding Data Types, Reference Variables, Typecasting
C++ Data Types
C++ Program - Integer
Code written in Visual Studio Code
// Program to illustrate the use of Built-in Data Types
// integer
#include<iostream>
#include<conio.h>
// stay connnected with our blog
using namespace std;
int c = 45;
int main()
{
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 << "Th sum is: " << c << endl;
cout << "The global c is: " << ::c;
getch();
return 0;
}
Enter the value of a:
8
Enter the value of b:
10
The sum is 18
The global c is 45
To be noted:
'::' is known as the scope resolution operator. It is used
to access a specific assigned value.
Code written in Visual Studio Code
C++ Program - float, double and long double literals
// Program to illustrate the use of Built-in Data Types
// float, double and long double literals
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float d = 3.14F;
long double e = 3.14L;
cout << "The size of 3.14 is " << sizeof(3.14) << endl;
cout << "The size of 3.14f is " << sizeof(3.14f) << endl;
cout << "The size of 3.14F is " << sizeof(3.14F) << endl;
cout << "The size of 3.14l is " << sizeof(3.14l) << endl;
cout << "The size of 3.14L is " << sizeof(3.14L) << endl;
cout << "The value of d is " << d << endl
<< "The value of e is " << e;
getch();
return 0;
}
The size of 3.14 is 8
The size of 3.14f is 4
The size of 3.14F is 4
The size of 3.14l is 12
The size of 3.14L is 12
The value of d is 3.14
The value of e is 3.14
Code written in Visual Studio Code
C++ Program - Reference Variables
// Program to illustrate the use of Reference Variables
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float x = 7071;
float &y = x;
cout << x << endl;
cout << y << endl; // we will get the same value as x
getch();
return 0;
}
7071
7071
Code written in Visual Studio Code
C++ Program - Typecasting
// Program to illustrate the use of Typecasting
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a = 71;
float b = 70.7;
cout << "The value of a is " << (float)a << endl;
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;
getch();
return 0;
}
The value of a is 71
The value of a is 71
The value of b is 70
The value of b is 70
The expression is 141.7
The expression is 141
The expression is 141
Coding: Where logic dances with creativity.
- Unknown