C++ 05: Type Casting

 Conversion of one type of value to another data type is called as type casting. C++ supports two type of typecasting .

1. Implicit Type Casting

2. Explicit Type Casting

Implicit Type Casting:

The type casting is automatically performed by the compiler is called as Implicit Type Casting.

Example

#include <iostream>
using namespace std;

main()
{
     int a = 3.14;
    cout << "The Value of a: " << a << endl;

    return 0;
}

Output:

The Value of a: 3


2.

#include <iostream>
using namespace std;

int main()
{
    char x = 66;
    cout << "The Value of x: " << x << endl;

    return 0;
}

Output:

The Value of x: B


Explicit Type Casting

The Typecasting specified by the programmer to the compiler is called as Explicit Type Casting.

Syntax:

(data_type)variable_name;

E.g.

#include <iostream>
using namespace std;

int main()
{
    int ab;
    cout << "Enter a Number: ";
    cin >> a;
    cout << "Enter b Number: ";
    cin >> b;

    float c = a / (float)b;
    cout << "Division is: " << c << endl;
    return 0;
}

Output:

Enter a Number: 9
Enter b Number: 2
Division is: 4.5


Follow cppwithayush.blogger.com for more Cpp related Concepts








Comments