C Language Basics
Learn the C toolchain and core syntax to express algorithms precisely and efficiently.
Content
Primitive Types and sizeof
Versions:
Watch & Learn
AI-discovered learning video
Sign in to watch the learning video for this topic.
Primitive Types and sizeof in C — The Small Things That Matter
"If data is the new oil, then primitive types are the refinery — small machinery, huge consequences."
You already know how a C program is stitched together (remember main, compilation, and linking?), and you’ve started building mental models about how data is represented. Now we go deeper: primitive types (the atoms of your program) and the trusty operator sizeof (the ruler that tells you how many bytes those atoms take up). This is the bridge between computational thinking about representation and practical, bug-avoiding C coding.
Why this matters
- Choosing the right type affects memory, performance, and correctness.
- sizeof helps you allocate memory correctly, avoid buffer overflows, and reason about how arrays and pointers behave.
- Many bugs (and portability headaches) come from assuming type sizes, or misusing sizeof on pointers vs arrays.
The primitive family tree (quick tour)
- Integer types: char, short, int, long, long long — each can be signed or unsigned.
- Floating types: float, double, long double.
- Other: _Bool (in <stdbool.h> as bool), and pointer types (e.g., int*).
Important: C does not require exact widths. The standard guarantees minimum ranges. The concrete sizes are platform-dependent (ABI, compiler, architecture): that's why sizeof matters.
A few truths to memorize
- sizeof(char) is always 1. That defines the C "byte".
- A byte (char) might be 8 bits on most machines, but consult
CHAR_BITin <limits.h> to be sure. - sizeof returns a value of type size_t (an unsigned integer type). Print it with
%zu.
sizeof: use it like a pro
Basic usage
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("sizeof(char) = %zu\n", sizeof(char));
printf("CHAR_BIT = %d bits per char\n", CHAR_BIT);
printf("sizeof(int) = %zu\n", sizeof(int));
printf("sizeof(double) = %zu\n", sizeof(double));
return 0;
}
Parentheses: when you need them
sizeof(type)— parentheses required when you pass a type.sizeof expression— parentheses optional for an expression:sizeof xworks.
Example: sizeof x + 1 is parsed as (sizeof x) + 1 (sizeof is a unary operator).
size_t and printf
sizeof yields a size_t. Use %zu in printf; using %d or %ld is wrong on some platforms and will give you grief.
Arrays, pointers, and the sizeof gotcha
This is where students get bitten. Consider:
int a[10];
printf("%zu\n", sizeof a); // 10 * sizeof(int)
printf("%zu\n", sizeof(a) / sizeof a[0]); // yields 10 -> neat!
int *p = a;
printf("%zu\n", sizeof p); // size of pointer (e.g., 8), NOT 10 * sizeof(int)
Why? Because arrays usually "decay" into pointers when passed to functions, or when used in many expressions. If you use sizeof inside the same scope where the array is declared, you get the total byte size. But once the array decays (e.g., in a function parameter int a[] or int *a), sizeof gives you the pointer-size — not the array length.
Tip: To compute number of elements in a static array in the same scope: sizeof a / sizeof a[0].
Structs, alignment, and padding — why sizeof can surprise you
CPU architectures have alignment requirements. Compilers insert padding to align fields, so
struct S { char c; int x; };
sizeof(struct S) may be 8 on a 64-bit system (1 byte for char + 3 bytes padding + 4 bytes for int) — not 5. Always trust sizeof, not your intuition.
Integer ranges, overflow, and signed vs unsigned
- The range of a type depends on its size and whether it is signed.
- Unsigned arithmetic wraps modulo 2^N (well-defined). Signed overflow is undefined behavior — avoid it.
Use limits.h to inspect constants like INT_MAX, LONG_MIN, etc.
Practical patterns you should use immediately
- Safe malloc idiom:
int *arr = malloc(n * sizeof *arr); // nicer than sizeof(int) and robust to type changes
Use
sizeoffor portability — never hardcode4or8unless you really mean a fixed width type (use stdint.h for those:int32_t,int64_t).When writing APIs: prefer passing both pointer and length (or use a struct) — don't rely on sizeof inside a function to find the caller's array length.
Special cases & advanced notes
sizeofon VLA (variable-length arrays, C99) is evaluated at run-time (not compile-time).sizeofon a function or onvoidis invalid.- Pointers on 64-bit systems usually have sizeof 8; on 32-bit systems, sizeof 4. This affects data layout and ABI (recall the compiler/linker/ABI conversation from earlier).
Quick reference table
| Expression | What sizeof returns |
|---|---|
sizeof(char) |
1 (by definition) |
sizeof(int) |
platform-dependent (common: 4) |
sizeof(long) |
platform-dependent (4 or 8) |
sizeof(double) |
usually 8 |
sizeof pointer |
size of pointer (4 or 8) |
sizeof array |
total bytes (length * element size) if in scope |
sizeof struct |
bytes including padding (>= sum of fields) |
Takeaways (the TL;DR that wins you exams and fixes bugs)
- sizeof tells you bytes. The "byte" is
charand hasCHAR_BITbits. - Don't assume sizes. Use sizeof or <stdint.h> fixed-width types for portability.
- Arrays vs pointers: sizeof(array) = total bytes, sizeof(pointer) = pointer size. Watch out for decay when passing arrays to functions.
- Use
malloc(n * sizeof *p)to avoid duplicating type names and reduce copy/paste errors. - Signed overflow is UB; unsigned wraps. Use the right type for your needs.
"This is the moment where the concept finally clicks: types are not just names — they are contracts with the machine about storage, alignment, and behavior. sizeof is your measuring tape."
Final homework (tiny but effective)
- Print
sizeofof all common types on your machine: char, short, int, long, long long, float, double, long double, pointers. - Create a
structwith mixed types and predict its size before checking withsizeof. - Write a function
size_t arrlen(int a[])and observe whatsizeoftells you inside it — then fix the API to accept length.
Do that, and your next debugging session will feel a lot less like spelunking in a dark cave and more like turning on the lights.
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!