Module 2 · Variables, Types, Input and Operators — topic 6 of 6
Type Conversion
C converts between types automatically in mixed expressions (implicit conversion) and lets you convert deliberately with casts. Learn the usual arithmetic conversions, integer promotion and the traps of narrowing and signed/unsigned mixing.
Learning objectives
- Apply integer promotion and the usual arithmetic conversions
- Use casts to control division and rounding
- Recognise data loss from narrowing and signed/unsigned comparisons
Definition
Type conversion changes a value from one type to another. Implicit conversions are applied by the compiler according to the usual arithmetic conversions and assignment rules; explicit conversions are written with a cast operator (type)expression.Introduction
When you write 5 / 2.0, C cannot divide an int by a double directly. It first converts the 5 to 5.0, then divides. These automatic conversions happen constantly and usually do what you want.
Sometimes they do not: assigning 3.99 to an int silently gives 3, and comparing -1 with an unsigned value can say −1 is larger. Understanding the rules lets you predict and control the result.
Why it matters
Conversion bugs compile without errors, so they survive until a strange result appears in production. Knowing where conversions happen is how you find them.
Casting is also a deliberate tool — for exact averages, for indexing with characters, and for working with malloc's void * in C++-compatible code.
Detailed explanation
Integer promotion: in expressions, char, short and _Bool values are first converted to int (or unsigned int if int can't hold every value). So char a = 100, b = 100; int c = a + b; gives 200 — the addition is done in int.
Usual arithmetic conversions: when two operands have different types, the “lower” one is converted to the “higher”: int → long → long long, and any integer → float → double → long double. If one operand is unsigned of the same rank, the signed one is converted to unsigned.
Assignment conversion: the right-hand value is converted to the left-hand type. Converting a floating value to an integer truncates toward zero ((int)-2.7 is −2). If the value does not fit, the result is undefined for floating → integer, and implementation-defined for a too-large integer stored in a signed type.
Explicit cast: (double)x converts x. A cast binds tightly: (double)a / b casts a first, then divides as double; (double)(a / b) divides as integers first.
Signed meets unsigned
In -1 < 1u, the -1 is converted to unsigned int — the huge value 4294967295 — so the comparison is false. Compilers warn with -Wextra (-Wsign-compare).
Syntax
- A cast is written
(type)before an expression. - Casting to a narrower type may lose information.
- Casts do not change the original variable; they produce a converted value.
Code examples
Implicit and explicit conversion
3 3.5 3.5 3.0
Truncation, not rounding
9 -9 10
Adding 0.5 before truncating rounds positive numbers; round() from <math.h> handles both signs.
More examples
Promotion, characters and the unsigned trap
char sum as int: 200 a -> A '7' as digit: 7 with a cast: -1 < 1 as unsigned, -1 becomes 4294967295
Step-by-step explanation
c1 + c2promotes bothchars toint, so 200 is computed without overflowing achar.letter - 32works because characters are small integers: 'a' is 97 and 'A' is 65 in ASCII.'7' - '0'converts a digit character to its numeric value — a common parsing idiom.- Casting
postointkeeps the comparison signed, so −1 < 1 as expected. - Converting −1 to
unsignedgives the maximum value, which is why mixing them silently breaks comparisons.
Expected output
Running “Promotion, characters and the unsigned trap” prints exactly:
Practical applications
- Parsers convert digit characters with
c - '0'. - Graphics code casts between
floatcoordinates andintpixel positions. - Security bugs often come from a negative length converted to a huge unsigned size.
Best practices
- Cast one operand, not the whole expression, when you need real division.
- Keep signed and unsigned values apart; enable
-Wextrato catch mixed comparisons. - Use
round,floorandceilfrom<math.h>when you mean rounding.
Common mistakes
Casting after the division
Avoid
Prefer
Parentheses force integer division first, so the fraction is already lost when the cast happens.
Comparing a signed index with sizeof
Avoid
Prefer
sizeof yields an unsigned size_t; i is converted to a huge unsigned value.
Important notes
- Converting a floating value that is out of the target integer's range (e.g.
(int)1e20) is undefined behaviour. 'a' - 32relies on ASCII; portable code usestoupperfrom<ctype.h>.
Key takeaways
- Small integer types are promoted to
intin expressions. - Mixed operands convert toward the wider or floating type; signed may become unsigned.
- Casting a floating value to an integer truncates toward zero.
Try it yourself
- 1Read a temperature in Fahrenheit as an integer and print Celsius with one decimal using
(f - 32) * 5 / 9.0. Then try5 / 9and explain the difference. - 2Write
toupper-like logic: convert a lower-case letter to upper case using arithmetic, only if it is between 'a' and 'z'.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
8 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?