Implicit Type Casting
Implicit Type Conversion is commonly referred to as ‘Automatic Type Conversion.’ It occurs automatically within the compiler without requiring external intervention from the user. This conversion typically happens when an expression involves more than one data type. In such scenarios, all variable data types involved are elevated to match the data type of the variable with the largest range or precision. This is also known as type promotion.
Integer promotion is the first step performed by the compiler. Subsequently, the compiler assesses whether the two operands in an expression possess different data types. In such cases, the conversion follows a hierarchy to determine the resulting data type. The hierarchy is as follows:
The compiler applies the conversion to match the data type based on this hierarchy, ensuring a consistent and predictable result in expressions with operands of different types.
#include <iostream>
using namespace std;
int main() {
int a = 'A';
// character to integer conversion
cout << a << "\n";
char ch = 65;
// integer to character conversion
cout << ch << "\n";
int x = 1000000;
int y = 1000000;
// Integers are multiplied and leads to overflow
long long z1 = x * y;
// Integer to Long Long conversion
long long z2 = (long long)x * y;
cout << z1 << " " << z2 << "\n";
int numerator = 15;
int denominator = 10;
// Integers are multiplied so result is an integer
int quotient1 = numerator/denominator;
// Integer to float conversion
float quotient2 = (float)numerator/denominator;
cout << quotient1 << " " << quotient2 << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
// Initialize integer variable 'a' with the ASCII value of 'A'
int a = 'A';
// Print the integer value of 'A' (65)
System.out.println(a);
// Initialize character variable 'ch' with ASCII value 65
char ch = 65;
// Print the character corresponding to ASCII value 65 ('A')
System.out.println(ch);
// Initialize integers x and y
int x = 1000000;
int y = 1000000;
// Integers are multiplied and leads to overflow
// Perform the multiplication and store the result in 'z1' (may cause overflow)
long z1 = x * y;
// Perform the multiplication with proper casting to long long (long) to prevent overflow
long z2 = (long)x * y;
// Print the results of both multiplications
System.out.println(z1 + " " + z2);
// Initialize numerator and denominator for division
int numerator = 15;
int denominator = 10;
// Integers are divided so result is an integer
// Perform integer division and store the result in 'quotient1'
int quotient1 = numerator / denominator;
// Perform floating-point division with proper casting to float to get the float quotient
float quotient2 = (float)numerator / denominator;
// Print both integer and float quotients
System.out.println(quotient1 + " " + quotient2);
}
}
# character to integer conversion
a = ord('A')
print(a)
# integer to character conversion
ch = chr(65)
print(ch)
# Integers are multiplied and may lead to overflow
x = 1000000
y = 1000000
z1 = x * y
# Integer to Long conversion
z2 = int(x) * y
print(z1, z2)
numerator = 15
denominator = 10
# Integers are divided so result is an integer
quotient1 = numerator // denominator
# Integer to float conversion
quotient2 = float(numerator) / denominator
print(quotient1, quotient2)
using System;
public class Program {
public static void Main(string[] args) {
// Initialize integer variable 'a' with the ASCII value of 'A'
int a = 'A';
// Print the integer value of 'A' (65)
Console.WriteLine(a);
// Initialize character variable 'ch' with ASCII value 65
char ch = (char)65;
// Print the character corresponding to ASCII value 65 ('A')
Console.WriteLine(ch);
// Initialize integers x and y
int x = 1000000;
int y = 1000000;
// Integers are multiplied and may lead to overflow
// Perform the multiplication and store the result in 'z1' (may cause overflow)
long z1 = (long)x * y;
// Print the result of multiplication (may cause overflow)
Console.WriteLine(z1);
// Initialize numerator and denominator for division
int numerator = 15;
int denominator = 10;
// Integers are divided so result is an integer
// Perform integer division and store the result in 'quotient1'
int quotient1 = numerator / denominator;
// Perform floating-point division with proper casting to float to get the float quotient
float quotient2 = (float)numerator / denominator;
// Print both integer and float quotients
Console.WriteLine(quotient1 + " " + quotient2);
}
}
class Main {
static main() {
// Initialize integer variable 'a' with the ASCII value of 'A'
let a = 'A'.charCodeAt(0);
// Print the integer value of 'A' (65)
console.log(a);
// Initialize character variable 'ch' with ASCII value 65
let ch = String.fromCharCode(65);
// Print the character corresponding to ASCII value 65 ('A')
console.log(ch);
// Initialize integers x and y
let x = 1000000;
let y = 1000000;
// Integers are multiplied and may lead to overflow
// Perform the multiplication and store the result in 'z1' (may cause overflow)
let z1 = x * y;
// Perform the multiplication with proper casting to BigInt to prevent overflow
let z2 = BigInt(x) * BigInt(y);
// Print the results of both multiplications
console.log(z1 + " " + z2);
// Initialize numerator and denominator for division
let numerator = 15;
let denominator = 10;
// Integers are divided so result is an integer
// Perform integer division and store the result in 'quotient1'
let quotient1 = Math.floor(numerator / denominator);
// Perform floating-point division to get the float quotient
let quotient2 = numerator / denominator;
// Print both integer and float quotients
console.log(quotient1 + " " + quotient2);
}
}
Main.main();
Output
65 A -727379968 1000000000000 1 1.5
Type Casting in Programming
In programming, variables hold data of specific types, such as integers, floating-point numbers, strings, and more. These data types determine how the computer interprets and manipulates the information. Type casting becomes necessary when you want to perform operations or assignments involving different data types. In this blog, we will explore type casting, its importance, and the various methods used in different programming languages.
Table of Content
- What is Type Casting
- Types of Type Casting
- Implicit Type Casting
- Explicit Type Casting
- Difference between Implicit and Explicit Type Casting
- Type Compatibility and Safety in Programming
- Type Casting between Basic Data Types
- Challenges and Best Practices of Type Casting in Programming
Contact Us