C++ Type Casting: Explicit and Implicit with Examples

C++ type casting

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

  1. Explicit C++ Type Casting.
  2. Implicit C++ Type Casting.



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 a 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 is 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:

C++ type casting

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 the expression “a/float(b)”, explicit C++ type casting is performed. The float(b) will return 2.0. the compiler will also convert the 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:

C++ type casting

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 type casting:

C++ type casting

Comment Statement:

The comment statement is a 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 the C++ program, comments are written by using double slashes “//” (we can also write comments between /* and */. The “/*” marks the beginning of a comment, while “*/” marks the end of comments). The comments can be given at any place in the source program. The following program uses comments.

Example testing comments statements in c++:

C++ type casting

Please note 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.



Related Article:

new and delete operator in C++ with examples

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: