When an expression mixes types, such as an int and a double, C converts them to a common type before calculating. Most of the time this does what you expect, but some rules surprise beginners and cause logical errors. This section states the rules precisely and shows how to take control with a cast.
Automatic (implicit) conversion
An implicit conversion happens automatically. For binary arithmetic operators C applies the usual arithmetic conversions:
Integer promotion:char and short operands are converted to int first.
If either operand is long double, the other becomes long double.
Otherwise, if either is double, the other becomes double.
Otherwise, if either is float, the other becomes float.
Otherwise both are integers: the operand of lower rank is converted to the higher-ranked type (int → long → long long), with special rules when one is unsigned.
Any floating type outranks every integer type, whatever their sizes. So in long_long_value + 1.0f, the 8-byte long long is converted to the 4-byte float.
Signed and unsigned mixing
When a signed int meets an unsigned int, the signed value is converted to unsigned. A negative number then becomes a very large positive one, so -1 < 1u is false (0). Compilers can warn about this with -Wextra (or -Wsign-compare). Avoid mixing signed and unsigned values in comparisons.
Conversion by assignment
In an assignment, the value is converted to the type of the variable on the left:
Assignment
Stored value
Note
int i = 4.6;
4
Fraction discarded (demotion)
float b = 20;
20.0
Promotion
char c = 65;
'A'
Integer code stored as a character
The cast operator
A cast converts a value to a named type explicitly:
Syntax
(type) expression
A cast is a unary operator, so it applies to the operand immediately after it. (float)a / 5 converts a and then divides in floating point; (float)(a / 5) divides integers first and only then converts the truncated result.
Key takeaways
The usual arithmetic conversions convert both operands to a common type before an operation.
char and short are promoted to int; float stays float in ANSI C.
Assignment converts the result to the left-hand type; float to int truncates.
(type) expression casts the operand immediately following it.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Why does if (-1 < 1u) behave as false? Explain and suggest a fix.
Show worked answer
1u is an unsigned int, so by the usual arithmetic conversions the int −1 is converted to unsigned int. −1 becomes the largest unsigned value (4,294,967,295 for 32 bits), and that is not less than 1, so the comparison gives 0. Fix: avoid mixing signed and unsigned operands, for example compare with 1 instead of 1u, or cast the unsigned value to a signed type when it is known to be small: -1 < (int)1u.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/data-types-operators-and-expressions/type-conversion-and-the-cast-operator/ to read this page.