C Programming - Beginner to Brilliance / Basics of C Programming

Type Conversions

🧾 Definiton

Type conversion is the process of changing one data type to another.

There are two types:

βœ… Implicit Type Conversion (done automatically by the compiler)

βœ‹ Explicit Type Conversion (done manually by the programmer – also called type casting)


βœ… Implicit Type Conversion (Automatic)

When you perform operations between different data types, the C compiler automatically converts the smaller or lower-ranked type to a larger or higher-ranked type to prevent data loss.

This is called type promotion.

πŸ” Real-Life Analogy:

Suppose you're putting water from a small cup (int) into a large glass (float) β€” it's easy and nothing is lost. This is implicit conversion.

But doing the reverse (glass to cup)? You'll spill water (i.e., data loss). That’s explicit conversion.


🧱 Type Promotion Hierarchy in C:

C promotes types in this order (from lower to higher):

char < short < int < unsigned int < long < float < double < long double


πŸ’‘ Example 1: Mixing int and float

int a = 10;
float b = 5.5;
float result = a + b;   // `a` is automatically promoted to float

πŸ” Behind the scenes:

10 (int) β†’ 10.0 (float) + 5.5 (float) = 15.5 (float

πŸ’‘ Example 2: char promoted to int

char letter = 'A';     // ASCII value = 65
int x = letter + 1;    // letter ('A') promoted to int β†’ 65 + 1 = 66

Even though letter is a char, it's promoted to int because int has higher rank.


βœ‹ Explicit Type Conversion (Type Casting)

When you (the programmer) forcefully convert one data type into another using type casting syntax, it's called explicit conversion.


🧾 Syntax:

(type_name) expression

πŸ“Œ Example:

float a = 3.14;
int b = (int)a;   // forcibly convert float to int

πŸ” Output:

b becomes 3, and decimal part (.14) is discarded.


🚨 Why Use Type Casting?

  1. To avoid warnings when mixing types

  2. To ensure precision or control data truncation

  3. To convert one type to another for calculation or display


πŸ’‘ Example: Avoid integer division

int a = 5, b = 2;
float result = a / b;                // result = 2.000000 ❌

float result2 = (float)a / b;        // βœ… 5.0 / 2 = 2.5

Without casting, a / b is integer division (5 / 2 = 2)

With casting, it becomes 5.0 / 2 = 2.5


πŸ’£ Watch Out! Data Loss Risk

int a = 100;
char b = (char)a;

Since char can store only 1 byte (0–255), values larger than that may overflow or behave unexpectedly.


πŸ§ͺ Type Conversion in Expressions

Sometimes you'll see mixed-type arithmetic, like this:

int x = 3;
float y = 4.2;
double z = x + y;

Here’s how C handles it:

  • x (int) is promoted to float

  • Then both are promoted to double

  • Final result stored in z


βœ… Tips to Remember

  • Implicit conversion promotes smaller types to larger ones.

  • Use explicit casting when:

    • You want to control rounding/truncation

    • You're doing division between integers

    • You're converting between incompatible types

  • Always be careful when casting to a smaller type β€” you might lose data!