Search⌘ K

Type Conversions

Explore how C handles type conversions through implicit rules and explicit casting. Understand potential pitfalls of automatic conversions, and learn to use library functions for converting strings to numeric types. This lesson helps you write clearer and safer C code by mastering these essential concepts.

There are two kinds of type conversion we need to talk about: automatic or implicit type conversion and explicit type conversion.

Implicit type conversion

The operators we have looked at can deal with different types. For example, we can apply the addition operator + to int as well as double values. It’s important to understand how operators deal with different types that appear in the same expression. There are rules in C that govern how operators convert different types, to evaluate the results of expressions.

For example, when a floating-point number is assigned to an integer variable in C, the decimal portion of the number gets truncated. On the other hand, when an ...