Chapter 6: Data Types, Sizes, and Limits
C is a statically typed language where every variable must have a type that dictates how many bits it occupies and how those bits are interpreted. Because C was designed for different architectures, the size of types can vary—a concept every system engineer must master.
I. Basic Data Types
C defines four primary categories of basic types:
| Type | Purpose | Common Size (64-bit) | Format Specifier |
|---|---|---|---|
char | Single character / Small integer | 1 byte (8 bits) | %c or %d |
int | Standard integer | 4 bytes (32 bits) | %d |
float | Single-precision floating point | 4 bytes (32 bits) | %f |
double | Double-precision floating point | 8 bytes (64 bits) | %lf |
II. Type Modifiers
Modifiers change the size or range of the basic types.
signedvsunsigned:signed(default): Half the range is negative, half is positive.unsigned: Only zero and positive values. Doubles the positive range.
shortvslong:short int: Typically 2 bytes.long int: Typically 4 or 8 bytes.long long int: Guaranteed at least 8 bytes (C99).
III. Fixed-Width Types (<stdint.h>)
Because int might be 16 bits on a microcontroller but 32 bits on a PC, portable code should use fixed-width types from stdint.h.
int8_t,uint8_t: 8-bit signed/unsigned.int32_t,uint32_t: 32-bit signed/unsigned.int64_t,uint64_t: 64-bit signed/unsigned.
IV. The sizeof Operator
sizeof is a compile-time operator that returns the size of a type or variable in bytes. It is essential for memory allocation.
#include <stdio.h>
int main() {
printf("Size of char: %zu byte\n", sizeof(char));
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of long long: %zu bytes\n", sizeof(long long));
return 0;
}
V. Range and Limits (<limits.h> & <float.h>)
Every type has a maximum and minimum value it can hold. Exceeding these causes Integer Overflow, a common source of security vulnerabilities.
INT_MAX: 2,147,483,647UINT_MAX: 4,294,967,295CHAR_BIT: Number of bits in a char (usually 8).
VI. Boolean and Void
_Bool(C99): A type that holds only 0 (false) or 1 (true). Often used via<stdbool.h>asbool.void: Represents the "absence of type." Used for functions that return nothing or for "generic pointers" (void *).