what do you mean by type conversion ?how is implicit conversion different from explicit conversion?
Answers
Answer:
Implicit type conversion is an automatic type conversion done by the compiler whenever data from different types is intermixed. ... When an implicit conversion is done, it is not just a reinterpretation of the expression's value but a conversion of that value to an equivalent value in the new type
Explanation:
Implicit conversion is the conversion in which a derived class is converted into a base class like int into a float type. ... Explicit conversion converts the base class into the derived class. We may need to perform the conversion on different other data types, to do that we take the help of the helper class
A type cast is basically a conversion from one type to another. There are two types of type conversion:
Implicit Type Conversion
Also known as ‘automatic type conversion’.
Done by the compiler on its own, without any external trigger from the user.
Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data.
All the data types of the variables are upgraded to the data type of the variable with largest data type.
bool -> char -> short int -> int ->
unsigned int -> long -> unsigned ->
long long -> float -> double -> long double
It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).
Example of Type Implicit Conversion:
// An example of implicit conversion
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
In a mixed expression, the result must be obtained into a specific data type. For this purpose, the data type needs to be converted into required type. It is known as type conversion.
In an implicit type conversion, the result of a mixed mode expression is obtained into the higher most data type without without any intervention of the user.
e.g.:
int a; float b; float c;
c = a + b;
In case of explicit type conversion, the data gets converted to another type as per user choice and the requirement.
e.g.:
int a; float b; double c;
b = (float)(a*c);