(b) What are the types of casting shown by the following examples: [2]
(i) char c = (char)120;
(ii) int x = ‘t';
Answers
Answered by
1
(i) char c = (char)120 is an Explicit type Cast and (ii) int x = ‘t' is an Implicit type Cast.
The types of casting shown by the following examples:
(i) char c = (char)120 = Explicit type Cast
(ii) int x = ‘t' = Implicit type Cast
Explicit type Cast :- Type casting is another name for this technique, which is users defined. In this case, the user can type cast the outcome to convert it to a certain data type.
- Benefits from Type Conversion:
- This is done in order to benefit from certain type representation or type hierarchy features.
- It makes it easier for us to compute expressions with variables of various data types.
Implicit Type Cast:- Implicit Type Cast is also referred as "automatic type conversion."
- without any external trigger from the user, performed by the compiler on its own. This typically occurs when there are multiple data types in an expression.
- To prevent data loss, type conversion (type promotion) is performed in these circumstances.
- All variable data types have been upgraded to the largest data type available.
- It is conceivable for implicit conversions to lose information, for signs to be lost (when signed is implicitly converted to unsigned), and for overflow to happen (when long long is implicitly converted to float).
Hence, (i) char c = (char)120 is an Explicit type Cast and (ii) int x = ‘t' is an Implicit type Cast.
#SPJ3
Similar questions