Data types in C: solved exercise with sizeof and casts
Data types in C: solved exercise
If you searched for data types in C solved exercise, this example helps you understand type sizes and basic casts.
Problem statement
Create a program that:
- prints sizes of common numeric types,
- casts
floattoint, - casts
inttodouble.
C solution
Expected output
Common mistakes
- Assuming fixed sizes across every platform.
- Mixing types without casts and losing precision.
- Ignoring truncation when converting
floattoint.
Practical use
Understanding types and casts in C helps prevent precision bugs, overflow issues, and unexpected behavior.
Recommended next exercise
Guided practice and full book
If you want a complete path with progressive difficulty:
FAQ
Is sizeof(int) always 4?
Not always. It is often 4 in practice, but it depends on architecture and compiler.
What happens when casting float to int?
The decimal part is truncated, not rounded.
When should I use explicit casts?
When you want predictable conversions and to avoid ambiguous implicit behavior.