C++ Type Casting
Type casting is converting an expression of a given one type into another type. It is denoted by (type). It is used to convert lower data type to higher data type to avoid data loss.
There are two types of type casting:
- Implicit conversion
- Explicit conversion

Syntax:-
(type)value;
Implicit conversion:
It does not require any operator for converted. They are automatically performed when a value is copied to a consistent type in the program.
The value has been promoted from int to double and we have not to specify any type-casting operator. This is known as standard conversion.
Example:-
#include <iostream> using namespace std; int main(){ int i=20; double p; p=i; cout << "Implicit value is " << p << endl; return 0; }
Explicit conversion:
Many conversions that involve a different interpretation of the value require an explicit conversion.
They are not automatically performed. It need to give explicit type casting in the program.
Example:-
#include <iostream> using namespace std; int main(){ int i=9, j=2; double z; z = (double) i / j; cout << "Value of Z : " << z << endl; return 0; }

Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.