# 08 - C Type Conversion - Converting Between Data Types

**URL:** https://buzzly.harislab.tech/t/08-c-type-conversion-converting-between-data-types/196
**Category:** C Programming Tutorials
**Tags:** c, basics
**Created:** [April 21, 2026, 2:42pm UTC](https://buzzly.harislab.tech/t/08-c-type-conversion-converting-between-data-types/196 "2026-04-21T14:42:23Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![Haris](https://buzzly.harislab.tech/user_avatar/buzzly.harislab.tech/haris/32/7_2.png) [@Haris](https://buzzly.harislab.tech/u/Haris)
#### Post date: [April 21, 2026, 2:42pm UTC](https://buzzly.harislab.tech/t/08-c-type-conversion-converting-between-data-types/196/1 "2026-04-21T14:42:23Z")

</div>

**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

```c
#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:**

```auto
x = 10
y = 10.00

```

### Example 2: Mathematical Operations

```c
#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:**

```auto
Result: 13.50

```

### Conversion Rules

When mixing types, the type hierarchy is:

```auto
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

```c
(newType) value

```

### Example 1: Float to Integer

```c
#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:**

```auto
Original: 19.99
Rounded: 19

```

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

### Example 2: Division with Different Types

```c
#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:**

```auto
Integer division: 3
Float division: 3.50

```

* * *

## Common Type Conversions

### Integer to Float

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

```

### Float to Integer

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

```

### Character to Integer (ASCII)

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

```

### Integer to Character (ASCII)

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

```

* * *

## Practical Examples

### Example 1: Temperature Conversion

```c
#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:**

```auto
Celsius: 25.5
Fahrenheit: 77.9

```

### Example 2: Calculate Average

```c
#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:**

```auto
Scores: 90, 85, 88
Average: 87.67

```

### Example 3: Rounding to Nearest Integer

```c
#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

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

```

### Warning 2: Integer Division

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

```

**Solution:**

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

```

### Warning 3: Overflow

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

```

* * *

## Practice Exercise

Create `conversion.c`:

```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;
}

```

* * *

### 📚 Navigation

| Previous | Next |
| --- | --- |
| [← C Data Types](https://buzzly.harislab.tech/t/07-c-data-types-integers-floats-characters/195/1) | [C Constants →](https://buzzly.harislab.tech/t/09-c-constants-creating-fixed-values/197/1) |

* * *

## ❓ Have Questions?

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

* * *

---

_[View the full topic](https://buzzly.harislab.tech/t/08-c-type-conversion-converting-between-data-types/196)._
