C++ Type Casting: Explicit and Implicit with examples
Table of Contents
C++ Type casting:
C++ Type casting- The process of converting a value from one data type to another during arithmetic operation is called casting or type of casting. C++ Type casting can be divided into two types
- Explicit C++ Type Casting.
- Implicit C++ Type Casting.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Explicit C++ type Casting:
The word “explicit” means ‘open’ or ‘clear’. In explicit C++ type casting, the data type in which the value is to be converted is clearly specified in the program. It is done by cast operator. The cast operator is unary operator. It converts the value of an expression into a value of the type specified.
The general form of the C++ type casting operator is as follows;
(type)expression
Where
(type): it indicates one of the C++ data type (or a user-defined data type) to which the value of the expression is to be converted.
Expression: it indicates a constant value, variable or an expression whose data type is to be converted.
For example, to convert a floating-point value 2.322 into an integer value, the statement are written as;
int x;
x=(int)2.322;
cout<<x;
After executing the above statements, the floating-point value will be converted into integer. The value assigned to variable ‘x’. the decimal portion will be truncated( or removed. The value 2 will be displayed on the screen.
Example: write a program that explains the Explicit C++ type casting:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> using namespace std; int main() { int a, b; a=15; b=2; cout<<a/b<<endl; cout<<a/float(b)<<endl; } |
In the above program, the expression “a/b” divides integer value ‘a’ by integer value ‘b’. the result of this expression is 7 (i.e. 15/2=7). In expression “a/float(b)”, explicit C++ type casting is performed. The float(b) will return 2.0. the compiler will also convert value of ‘a’ into float type (i.e. 15.0); because in this expression float is the higher priority data type. So the result of 15.0/2.0 will be 7.5
Example: write a program that performs division between two real values and finds the remainder by using explicit C++ type casting:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { float x, y; int r; x=15.6; y=6.3; r=(int)x%(int)y; cout<<" Remainder is "<<r; } |
The above program uses the explicit C++ type casting to convert the real values into integer value and then finds the remainder of two integer values using modulus operator (%). The value returned by “(int)x” will be 15, while “(int)y” will be 6. So the result of 15%6 will be 3.
Implicit C++ Type Casting:
The word “implicit” means ‘understood’ or ‘embedded’. In implicit C++ type casting, the data type in which the value is to be converted is not specified in the program. It is automatically done by the C++ compiler.
When constant values and variables of different types are mixed in an expression, they are converted into the same type. The compiler will convert all operands of lower order data type into higher order data type. The order of data types is shown in the following table.
Data Type | Order |
Long double | Highest |
Double | |
Float | |
Long | |
Int | |
Short | |
Char | lowest |
In the above table, ‘long double’ data type has the highest order, while ‘char’ data type has the lowest order. For example, if an expression has highest order operand of data type ‘int’, then all ‘char’ and ‘short int’ will be converted into ‘int’.
Similarly, in the assignment statement when a value of one data type is assigned to a variable of another data type, the type of the value being assigned is converted into the data type of variable to the left side of assignment operator. For example;
int x;
float y= 49.723;
x=y;
The data type of the variable ‘x’ is ‘int’. when the value of ‘y’ is assigned to ‘x’, it is converted into integer type and value 49 will be assigned to ‘x’. the decimal portion of ‘y’ will be truncated.
Example: write a program that separated the integral and fractional parts of a given real number and displays the result on screen using implicit C++ type casting:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> using namespace std; int main() { float a= 15.58971, b; int n; b=a-n; cout<<" Real number is :"<<a<<endl; cout<<" Integral part = "<<n<<endl; cout<<" Fractional part = "<<b<<endl; } |
Comment Statement:
The comment statement is non-executable statement. It is used to add remarks or comments in a program. C++ compiler ignores the comments written in the source code. It means that comment lines are not translated into machine code.
Comments are used to increase the readability of the program. Using comments in the source code, a user can easily understand the logic of the program. The comments also help during debugging and modification of the program.
In C++ program, comments are written by using double slashes “//” (we can also write comments between /* and /. The “/” marks the beginning of comment, while “*/” marks the end of comments). The comments can be given at any place in the source program. Following program uses comments.
Example testing comments statements in c++:
1 2 3 4 5 6 7 8 9 10 11 |
//Program to write a message on screen #include <iostream> //header file using namespace std; int main() //main function { //statement to print a message cout<<" testing comments statement in c++"; } |
Please not that comments are not tokens. The compiler changes each comment into a single blank space before converting the source code into object code. Therefore comments do not increase the size of the executable file.