08 - C Type Conversion - Converting Between Data Types

Learn how to convert values between different data types


What is this topic?

This guide explains C Type Conversion - Convert Between Data Types in simple terms, what it does, and how to use it in real C programs.

Why We Need It

  • It helps you write correct and reliable C code.
  • It makes your programs easier to read and maintain.
  • It is used in real projects and interviews.
  • It reduces common beginner mistakes.
  • It builds a strong foundation for advanced topics.

Use Cases

  • Building practical C programs step by step.
  • Solving real coding tasks with clean logic.
  • Preparing for exams, interviews, and projects.
  • Understanding and improving existing C code.

What is Type Conversion?

Type conversion is changing a value from one data type to another. For example, converting an integer to a float, or a float to an integer.

Why Convert Types?

  • Mathematical operations - Mix integers and floats
  • Function requirements - Function expects specific type
  • Rounding - Convert float to int
  • Compatibility - Work with different data types

Implicit Conversion (Automatic)

C automatically converts types in certain situations.

Example 1: Integer to Float

#include <stdio.h>

int main() {
    int x = 10;
    float y = x;  // Automatically converts int to float
    
    printf("x = %d\n", x);
    printf("y = %.2f\n", y);
    
    return 0;
}

Output:

x = 10
y = 10.00

Example 2: Mathematical Operations

#include <stdio.h>

int main() {
    int a = 10;
    float b = 3.5;
    
    float result = a + b;  // int converts to float
    printf("Result: %.2f\n", result);
    
    return 0;
}

Output:

Result: 13.50

Conversion Rules

When mixing types, the type hierarchy is:

char → int → float → double

The smaller type converts to the larger type.


Explicit Conversion (Casting)

You can manually convert types using casting syntax.

Casting Syntax

(newType) value

Example 1: Float to Integer

#include <stdio.h>

int main() {
    float price = 19.99;
    int roundedPrice = (int) price;  // Cast to int
    
    printf("Original: %.2f\n", price);
    printf("Rounded: %d\n", roundedPrice);
    
    return 0;
}

Output:

Original: 19.99
Rounded: 19

:warning: Notice: 19.99 becomes 19, not 20. Casting truncates (cuts off) the decimal.

Example 2: Division with Different Types

#include <stdio.h>

int main() {
    int x = 7;
    int y = 2;
    
    // Integer division (no decimal)
    int result1 = x / y;
    printf("Integer division: %d\n", result1);  // 3
    
    // Float division (with decimal)
    float result2 = (float) x / y;
    printf("Float division: %.2f\n", result2);  // 3.50
    
    return 0;
}

Output:

Integer division: 3
Float division: 3.50

Common Type Conversions

Integer to Float

int num = 42;
float decimal = (float) num;  // 42.00

Float to Integer

float price = 99.99;
int amount = (int) price;  // 99

Character to Integer (ASCII)

char letter = 'A';
int value = (int) letter;  // 65
printf("%d\n", value);

Integer to Character (ASCII)

int ascii = 65;
char letter = (char) ascii;  // 'A'
printf("%c\n", letter);

Practical Examples

Example 1: Temperature Conversion

#include <stdio.h>

int main() {
    float celsius = 25.5;
    
    // Convert to Fahrenheit
    float fahrenheit = (celsius * 9/5) + 32;
    
    printf("Celsius: %.1f\n", celsius);
    printf("Fahrenheit: %.1f\n", fahrenheit);
    
    return 0;
}

Output:

Celsius: 25.5
Fahrenheit: 77.9

Example 2: Calculate Average

#include <stdio.h>

int main() {
    int score1 = 90;
    int score2 = 85;
    int score3 = 88;
    
    // Calculate average (must convert to float)
    float average = (float)(score1 + score2 + score3) / 3;
    
    printf("Scores: %d, %d, %d\n", score1, score2, score3);
    printf("Average: %.2f\n", average);
    
    return 0;
}

Output:

Scores: 90, 85, 88
Average: 87.67

Example 3: Rounding to Nearest Integer

#include <stdio.h>

int main() {
    float price = 19.99;
    
    // Simple truncation
    int truncated = (int) price;
    printf("Truncated: %d\n", truncated);  // 19
    
    // Rounding (add 0.5 then cast)
    int rounded = (int)(price + 0.5);
    printf("Rounded: %d\n", rounded);  // 20
    
    return 0;
}

Type Conversion Table

From To Method Example
int float Implicit or (float) float x = 5;
float int Cast with (int) int x = (int) 5.9;
char int Cast with (int) int x = (int) 'A';
int char Cast with (char) char x = (char) 65;
double float Cast with (float) float x = (float) 3.14;

Important Warnings

Warning 1: Data Loss with Casting

float price = 99.99;
int amount = (int) price;  // Lost .99

Warning 2: Integer Division

int result = 7 / 2;  // Gives 3, not 3.5
// Both are integers, so result is integer

Solution:

float result = (float) 7 / 2;  // Gives 3.5

Warning 3: Overflow

int max = 2147483647;  // Maximum int value
int overflow = max + 1;  // Wraps around (bad!)

Practice Exercise

Create conversion.c:

#include <stdio.h>

int main() {
    // Integer values
    int apples = 5;
    int oranges = 3;
    
    // Calculate average (need float conversion)
    float average = (float)(apples + oranges) / 2;
    
    printf("Apples: %d\n", apples);
    printf("Oranges: %d\n", oranges);
    printf("Average: %.2f\n", average);
    
    // Convert float to int
    int total_int = (int) average;
    printf("Rounded Average: %d\n", total_int);
    
    return 0;
}

:books: Navigation


:red_question_mark: Have Questions?

Comment below and we’ll help you! Struggling with type conversion? Ask your questions below!