Type Casting & Type Conversion in C – BKNMU Junagadh | BCA Sem 1 Notes



BCA Sem 1  |  Unit 1  |  Computer Concepts & C Programming

Type Casting & Type Conversion in C — BKNMU Junagadh

Implicit Conversion vs Explicit Casting, Promotion Rules & Potential Data Loss

BKNMU Junagadh NEP 2020 Type Casting Implicit Conversion Explicit Conversion
In simple words: Type Conversion is the process of converting a variable or expression from one data type to another. In C programming, this conversion happens in two ways: automatically by the compiler (Implicit Type Conversion or Type Promotion) or manually by the programmer (Explicit Type Conversion or Type Casting).
📖 Implicit Conversion vs. Explicit Type Casting

To write accurate calculations in C, you must understand the key difference between these two approaches:

Automatic by Compiler

1. Implicit Type Conversion

Occurs automatically when mixing different data types in an expression. Converts smaller data types to larger ones to prevent data loss (Type Promotion).

(target_type) expression

2. Explicit Type Casting

Performed manually by the programmer using the cast operator (type) to forcibly convert a value into a specific data type.

🎨 Automatic Type Promotion Hierarchy

Hierarchy Order for Implicit Conversion (Lower to Higher Type):

char / short int unsigned int long float double

When two operands of different types meet in an expression, C automatically promotes the operand of lower rank to the rank of the higher operand.

1️⃣ Comprehensive Comparison Table

Here is a side-by-side comparison between Implicit Type Conversion and Explicit Type Casting as required for university theory exams:

Comparison FeatureImplicit Type ConversionExplicit Type Casting
Execution MethodDone automatically by the C compiler during program execution without programmer intervention.Done manually by the programmer using cast operators in the code source.
Alternative NameAlso known as Automatic Conversion or Type Promotion.Also known as Narrowing Conversion or Forced Casting.
Operator RequirementDoes not require any special cast operator or syntax.Requires the Cast Operator syntax: (data_type) expression.
Data Loss RiskNo data loss occurs because lower types are promoted to higher types.Risk of data truncation or precision loss when converting higher types to lower types.
Common ScenarioEvaluated in mixed-mode expressions like int + float (result becomes float).Forcing integer division to yield floating-point output like (float)5 / 2.
ℹ️ Classic Integer Division Trap: In C, evaluating 5 / 2 yields 2 (integer division truncates decimal part!). To get the actual decimal result 2.5, you MUST use type casting: (float)5 / 2.
💻 Working Code Examples

Example 1: Implicit Type Conversion

#include <stdio.h> int main() { int count = 10; char letter = 'A'; // ASCII value of 'A' is 65 float price = 15.5; // 'letter' (char) is automatically promoted to int (65) int sum = count + letter; // 10 + 65 = 75 // 'count' (int) is promoted to float (10.0) before addition float total = count + price; // 10.0 + 15.5 = 25.5 printf("Sum (int + char): %d\n", sum); printf("Total (int + float): %.1f\n", total); return 0; }

Example 2: Explicit Type Casting

#include <stdio.h> int main() { int total_marks = 475; int total_subjects = 6; // Without casting: 475 / 6 = 79 (truncates decimal!) float avg_wrong = total_marks / total_subjects; // With explicit type casting: (float)475 / 6 = 79.166664 float avg_correct = (float)total_marks / total_subjects; printf("Incorrect Average: %.2f\n", avg_wrong); printf("Correct Average: %.2f\n", avg_correct); return 0; }
💡 Exam Syntax Tip: The Cast Operator syntax is (target_type) expression. For example, (double) x / y casts x to double before division occurs.
🔄 When to Use Explicit Type Casting
1

1. Calculating Averages or Ratios

When dividing two integers where exact fractional decimal values are required (e.g., percentage calculations).

2

2. Converting Floating-Point to Integers

When you intentionally want to discard the fractional part of a floating-point number (e.g., (int) 9.99 results in 9).

3

3. Working with Pointers (Void Pointers)

Casting dynamic memory returned by functions like malloc() to specific pointer types.

⚠️ Data Truncation Warning: Explicitly casting a larger data type into a smaller one (e.g., double to int) leads to truncation of fractional digits and potential overflow if the integer value exceeds the destination range!
📝 Quick Revision — Key Exam Points
  • Type Conversion: Process of changing a variable value from one data type to another.
  • Implicit Conversion: Automatic type promotion by C compiler from smaller to larger type.
  • Explicit Type Casting: Manual conversion by programmer using (type) value syntax.
  • Type Promotion Order: charintfloatdouble.
  • Cast Operator: Unary operator with high precedence rank.
  • Data Loss: Implicit conversion avoids data loss; explicit casting down can lose data precision.

Post a Comment

Thanks for comment.

Previous Post Next Post