prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

C++ Programming Part 5

Rajjit May 20th, 2023 5 mins read
post-14

TOPIC: Understanding Data Types, Reference Variables, Typecasting

C++ Data Types

  • In this blog, we will discuss about Built-in Data types such as integers, float, double, long double literals.
  • Integers: It includes the numerals which are not fractions.
    Example: int c = 45;
  • Float: It includes the numbers with decimals.
    Example: float d = 3.24;
  • Double: It is an extended form of float.
    Example: float d = 3.14F;
  • Long Double: It is an extended form of double.
    Example: long double e = 3.14L;
copy
This is the code about Built-in 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;
}
Output:
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;
}
Output:
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

  • Reference Variables are the variables which are used to access the values of other variables using reference.

// 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;
}
Output:
7071
7071

Code written in Visual Studio Code

C++ Program - Typecasting

  • We cannot perform operations among different data types. So in order to make it possible, we can use the typecasting method.
  • Here, a value with a data type can be changed to a different data type so that the operations can be performed without any errors. For example, we can change the integer data type to a float. By doing so, we can add or multiply or subtract two floats, in which one was an integer.
  • So, typecasting provides a way to perform different types of operations among different data types.
  • We can use (float)x or float(x) for typecasting into float. Similarly, we can use (int)x or int(x) for typecasting into integer.

// 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;
}
Output:
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

That's the end for this blog. More coming soon!

Coding: Where logic dances with creativity.
- Unknown