0% found this document useful (0 votes)
26 views5 pages

Implicit vs Explicit Type Casting

The document explains implicit and explicit type casting in programming, highlighting that implicit casting is automatic conversion from a smaller to a larger data type, while explicit casting requires manual conversion using a cast operator. It provides examples for both types, including assignments and arithmetic operations for implicit casting, and manual conversions for explicit casting. Additionally, it summarizes the differences between the two types of casting in a table format.

Uploaded by

hifsa bushra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Implicit vs Explicit Type Casting

The document explains implicit and explicit type casting in programming, highlighting that implicit casting is automatic conversion from a smaller to a larger data type, while explicit casting requires manual conversion using a cast operator. It provides examples for both types, including assignments and arithmetic operations for implicit casting, and manual conversions for explicit casting. Additionally, it summarizes the differences between the two types of casting in a table format.

Uploaded by

hifsa bushra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SLO 11.2.

6 Write a program to show the implicit and


explicit type casting of variables.

Implicit Type Casting (Automatic Conversion)


Definition:
Implicit type casting is when the compiler automatically converts a smaller data type to a
larger data type without you doing anything.

 Also called type promotion.


 Happens in assignments or arithmetic operations.

Syntax
larger_type variable = smaller_type_variable; // automatic conversion

or in arithmetic:

larger_type result = smaller_type1 + smaller_type2; // smaller type


promoted automatically

Example 1: Assignment
#include <iostream>
using namespace std;

int main() {
int a = 10;
double b;

b = a; // int automatically converted to double

cout << "Integer a = " << a << endl;


cout << "Double b after implicit conversion = " << b << endl;

return 0;
}

Output:

Integer a = 10
Double b after implicit conversion = 10

Explanation: int is automatically converted to double when assigned to b.


Example 2: Arithmetic Operation
#include <iostream>
using namespace std;

int main() {
int a = 5;
double b = 2.5;

double result = a + b; // int promoted to double automatically

cout << "Result of a + b = " << result << endl;

return 0;
}

Output:

Result of a + b = 7.5

Explanation: a (int) is converted to double before addition.

Example 3: Mixed Operations


#include <iostream>
using namespace std;

int main() {
char c = 'A'; // ASCII value 65
int num;

num = c + 5; // char automatically converted to int

cout << "Character c = " << c << endl;


cout << "Result of c + 5 = " << num << endl;

return 0;
}

Output:

Character c = A
Result of c + 5 = 70

Explanation:

 char is converted to its ASCII value (A = 65) before addition.


These 3 examples cover:

1. Assignment (int → double)


2. Arithmetic operation (int + double)
3. Character promotion (char → int)

Explicit Type Casting (Manual Conversion)


Definition:
Explicit type casting is when you manually convert a variable from one data type to another
using a cast operator.

 Also called type casting.


 Syntax: (type)variable
 Useful when converting a larger type to smaller type or forcing a conversion that
compiler won’t do automatically.

Syntax
// Manual conversion
type new_variable = (type)variable;

// Example in arithmetic
int result = (int)3.78; // double converted to int manually

Example 1: Double to Int


#include <iostream>
using namespace std;

int main() {
double x = 9.78;
int y;

y = (int)x; // explicit conversion from double to int

cout << "Double x = " << x << endl;


cout << "Integer y after explicit conversion = " << y << endl;

return 0;
}

Output:

Double x = 9.78
Integer y after explicit conversion = 9

Explanation:

 x is a double.
 (int)x converts it manually to integer.
 Decimal part .78 is truncated, not rounded.

Example 2: Float to Int in Arithmetic


#include <iostream>
using namespace std;

int main() {
float a = 5.7;
int b = 2;

int result = (int)a + b; // a is explicitly converted to int


before addition

cout << "Result of (int)a + b = " << result << endl;

return 0;
}

Output:

Result of (int)a + b = 7

Explanation:

 a = 5.7 is converted to 5 before addition.


 Then 5 + 2 = 7.

Example 3: Char to Int


#include <iostream>
using namespace std;

int main() {
char c = 'B'; // ASCII 66
int num;

num = (int)c; // explicit conversion from char to int

cout << "Character c = " << c << endl;


cout << "Integer value of c = " << num << endl;

return 0;
}

Output:

Character c = B
Integer value of c = 66

Explanation:

 c is a char.
 (int)c converts it to its ASCII integer value.

✅ Summary
Feature Implicit Casting Explicit Casting

Conversion
Automatic Manual
type

Syntax larger_type var = smaller; (type)variable

Example double d = 5; int x = (int)5.7;

Narrowing or forced
Use case Safe widening conversions
conversion

You might also like