Module 2 · Variables, Types, Input and Operators — topic 3 of 6
Data Types
C's built-in types — char, int, float, double and their short, long, signed and unsigned variants — decide how many bytes a value occupies and what range it can hold.
Learning objectives
- List the basic C types with typical sizes and ranges
- Use sizeof and <limits.h> to inspect types
- Explain signed vs unsigned and integer overflow
Definition
A data type defines a set of values, their in-memory representation and size, and the operations that may be performed on them. C's basic types are the integer types (char,short,int,long,long long,_Bool) and the real floating types (float,double,long double).
Introduction
Every value in memory is just bits. The type tells the compiler how to interpret those bits: as a whole number, a character code or a floating-point number, and how many bytes to read.
The C standard only fixes minimum ranges — for example an int must hold at least −32767 to 32767. On today's 64-bit desktops an int is 4 bytes, but on some microcontrollers it is 2. Good C code checks sizes rather than assuming them.
Why it matters
Choosing a type too small causes overflow — a classic source of real-world failures. Choosing floating point for money causes rounding errors. Knowing the types lets you pick the right one deliberately.
Questions such as “what is sizeof(char)?”, “what happens on unsigned overflow?” and “why is 0.1 + 0.2 != 0.3?” are staples of placement tests.
Detailed explanation
sizeof gives the size of a type or object in bytes as a size_t; print it with %zu. By definition sizeof(char) is 1.
Unsigned types cannot be negative. Arithmetic on them wraps around modulo 2ⁿ, which is well defined. Signed overflow (e.g. INT_MAX + 1) is undefined behaviour — the compiler may do anything.
Floating-point numbers store a binary fraction and an exponent, so most decimals (like 0.1) are approximations. Prefer double over float for accuracy, and never compare floating results with ==.
For exact widths use <stdint.h>: int32_t, uint8_t, int64_t. For true/false use <stdbool.h>, which provides bool, true and false.
Syntax
- Suffixes
u,LLandfset the literal's type. sizeofis an operator, not a function; parentheses are required only around type names.size_tis the unsigned type used for sizes and counts.
Code examples
Sizes on this machine
char 1 int 4 double 8
char is always 1; the others are typical for 32- and 64-bit desktops.
Limits from <limits.h>
INT_MAX = 2147483647 INT_MIN = -2147483648 UINT_MAX = 4294967295
More examples
Unsigned wrap-around and float precision
UINT_MAX + 1 = 0 0u - 1 = 4294967295 0.1 + 0.2 = 0.30000000000000004 equal to 0.3? 0
Step-by-step explanation
ustarts at the largestunsigned int; adding 1 wraps around to 0 by definition.- Subtracting 1 from an unsigned 0 wraps the other way to 4294967295.
0.1and0.2are stored as binary approximations, so their sum is slightly more than 0.3.%.17freveals the hidden digits;x == 0.3is therefore false and prints 0.
Expected output
Running “Unsigned wrap-around and float precision” prints exactly:
Practical applications
- Network protocols use exact-width types such as
uint16_tfor port numbers. - Image libraries store pixel channels as
uint8_t(0–255). - Banking systems store money as integers of paise, never as
float, to avoid rounding errors. - The Ariane 5 rocket failure (1996) was caused by converting a 64-bit float to a 16-bit integer that overflowed.
Best practices
- Use
intfor ordinary counting,doublefor real numbers,size_tfor sizes and indexes. - Use
<stdint.h>types when the exact width matters (files, networks, hardware). - Never store currency in
float; use integer units such as paise.
Common mistakes
Comparing floats with ==
Avoid
Prefer
Floating-point results carry rounding error. Compare with a small tolerance.
Wrong printf specifier
Avoid
Prefer
A mismatched format specifier is undefined behaviour and usually prints garbage. -Wall warns about it.
Important notes
- Whether plain
charis signed or unsigned is implementation-defined; usesigned charorunsigned charwhen it matters. longis 8 bytes on Linux and macOS but only 4 bytes on 64-bit Windows; uselong longorint64_tfor portable 64-bit integers.
Key takeaways
- Types fix size, range and operations; the standard gives minimums, not exact sizes.
- Unsigned overflow wraps; signed overflow is undefined behaviour.
- Floating point is approximate — don't compare with
==.
Try it yourself
- 1Print the size of
short,long,long long,floatandlong doubleon your computer. - 2Store 300 in an
unsigned charand print it. Explain the result.Show hint
An unsigned char holds 0–255; think modulo 256.
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 `sizeof(char)` guaranteed to be?
- BeginnerWhich format specifier prints a `long long` value?
- BeginnerWhy should money not be stored in a `float`?