Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 6 of 10
Type Conversions & Integer Promotion
C performs automatic type conversions in two scenarios: **implicit conversion** (when operands of different types are combined in an expression) and **explicit conversion** via casts. The rules of **integer promotion** and the **usual arithmetic conversions** are a major source of subtle bugs - especially when mixing signed and unsigned types.
Implicit Conversion & Integer Promotion Rules
#include <stdio.h>
int main(void) {
/* ── Integer promotion ───────────────────────────────
Any type smaller than int (char, short) is promoted
to int before arithmetic operations. */
char a = 100;
char b = 200; /* 200 fits in unsigned char, not signed char */
int c = a + b; /* a and b promoted to int before addition */
printf("a + b = %d
", c); /* 300 - no overflow because int */
/* ── Integer division (common surprise) ─────────────*/
int num = 7, den = 2;
double result = num / den; /* 3.0 NOT 3.5 - division done in int first */
printf("7 / 2 (int div) = %.1f
", result); /* 3.0 */
double correct = (double)num / den; /* explicit cast BEFORE division */
printf("7 / 2 (correct) = %.1f
", correct); /* 3.5 */
/* ── Usual arithmetic conversions (hierarchy) ───────
When two different types meet in an expression:
1. If either is long double → both become long double
2. If either is double → both become double
3. If either is float → both become float
4. Integer promotions apply, then:
- unsigned wins over signed if same rank */
int si = -1;
unsigned int ui = 1;
/* si < ui should be true, but... */
if (si < ui) printf("si < ui: true
");
else printf("si < ui: FALSE - -1 became 4294967295!
");
/* -1 as unsigned int = UINT_MAX = 4294967295 on 32-bit */
/* ── Explicit cast - say what you mean ─────────────*/
unsigned char byte = 255;
int extended = (int)byte; /* safe: 255 fits in int */
printf("byte as int: %d
", extended);
return 0;
}Common Mistakes
- Signed/unsigned comparison - `if (signed_var < unsigned_var)` promotes `signed_var` to unsigned. If it was negative, it becomes a huge positive number. Use `-Wsign-compare` (included in `-Wextra`) to get a warning.
- Truncation on assignment - `char c = 300;` silently truncates to `300 & 0xFF = 44`. The compiler may warn with `-Wconversion`. Always be explicit when converting to a narrower type.
- Forgetting that `char` may be signed - on most x86 systems `char` is signed. On ARM it's often unsigned by default. Code that relies on `char` signedness is not portable. Use `signed char` or `unsigned char` explicitly.
Tip
Tip
Practice Type Conversions Integer Promotion in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Implicit integer promotion widens small types to int before arithmetic operations
Practice Task
Note
Practice Task - (1) Write a working example of Type Conversions Integer Promotion from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Type Conversions Integer Promotion is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- C performs automatic type conversions in two scenarios: **implicit conversion** (when operands of different types are combined in an expression) and **explicit conversion** via casts.
- Signed/unsigned comparison - `if (signed_var < unsigned_var)` promotes `signed_var` to unsigned. If it was negative, it becomes a huge positive number. Use `-Wsign-compare` (included in `-Wextra`) to get a warning.
- Truncation on assignment - `char c = 300;` silently truncates to `300 & 0xFF = 44`. The compiler may warn with `-Wconversion`. Always be explicit when converting to a narrower type.
- Forgetting that `char` may be signed - on most x86 systems `char` is signed. On ARM it's often unsigned by default. Code that relies on `char` signedness is not portable. Use `signed char` or `unsigned char` explicitly.
Finished this lesson?