Loading…
Loading…
Charting your lesson…
Opening your lesson…
Data Types, Sizes & Storage Classes in C
Lesson 1 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Ask "how big is an int?" in an interview and the wrong answer is a confident "4 bytes". The right answer starts with "it depends on the platform" — and that instinct is what this lesson builds.
Core idea: a type tells the compiler two things — how many bytes to reserve and how to interpret the bit pattern stored there. A storage class answers three orthogonal questions about a variable: where it lives (storage duration), who can see it (scope), and whether the name links across files (linkage).
Step 2/ 61 min
C ships with char, int, float, double and the modifiers short, long, signed, unsigned. A char is exactly 1 byte by definition, but a byte need not be 8 bits (though it almost always is). The only guarantees the standard makes are relative: sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long), with minimum ranges (an int holds at least ±32767).
printf("%zu\n", sizeof(int)); // 4 on most 64-bit systems, but NOT guaranteed
The compiler and target ABI decide actual widths. On the common LP64 model (Linux/macOS 64-bit): int = 4, long = 8, pointer = 8. On LLP64 (64-bit Windows): long = 4, pointer = 8. Portable code that needs a known width uses <stdint.h>: int32_t, uint64_t. Why it matters: assuming long is 8 bytes and shifting a bitmask by 40 is undefined on Windows.
Four keywords cover the axes of duration/scope/linkage:
| Class | Duration | Scope | Linkage |
|---|---|---|---|
auto (default local) |
automatic (stack) | block | none |
static (local) |
whole program | block | none |
static (file-level) |
whole program | file | internal |
extern |
whole program | file | external |
register |
automatic | block | none (hint only) |
A static local keeps its value between calls because it lives in the data segment, not on the stack. A static global is invisible to other translation units — the classic way to make a file-private function.
Step 3/ 61 min
int counter() {
static int n = 0; // initialised ONCE, at load time
n++;
return n;
}
// counter() called three times returns 1, 2, 3
The static storage means n is not reset on each call. Remove static and every call returns 1, because an auto local is re-created on the stack each time.
Step 4/ 61 min
GATE and product-company screens love: "output of sizeof expressions", "what does static before a global mean" (answer: internal linkage, not lifetime here — globals already live forever), and integer-overflow traps. Knowing LP64 vs LLP64 signals real systems experience.
Step 5/ 61 min
static is overloaded: on a local it changes duration; on a global/function it changes linkage. Same keyword, different job.sizeof returns size_t (unsigned) — print with %zu, and never write sizeof(a) - 1 < 0 (unsigned never goes negative).Believing static int n; inside a function re-initialises to 0 on every call. The initialiser runs exactly once (at program load); the variable retains its last value across calls. This bug shows up as counters or caches that "won't reset" — or, conversely, that unexpectedly persist.
<stdint.h> (int32_t…) when you need a fixed width.static local → persists across calls; static global/function → internal linkage (file-private).extern declares a name defined elsewhere; register is a (usually ignored) speed hint.sizeof yields unsigned size_t.int is 4 bytes, and remember static wears two hats — persistence for locals, privacy for globals.Step 6/ 6· Worked examples2 min
Problem
On a typical 64-bit LP64 system (int = 4 bytes, long = 8 bytes), predict the exact output of this program and explain each line.
#include <stdio.h>
int counter(void) { static int n = 0; return ++n; }
int main(void) {
unsigned char c = 250;
c = c + 10; // (A)
printf("A=%d\n", c);
int x = -1;
unsigned int u = 1;
printf("B=%d\n", x < u); // (B)
printf("C=%d %d %d\n", counter(), counter(), counter()); // (C) left-to-right assumed
printf("D=%zu\n", sizeof(3 + 'a')); // (D)
return 0;
}
Try it on paper first.
Recall
Keep this
Data types and storage classes are the single most common C-fundamentals topic in campus written tests and viva rounds. Expect "predict the output" and "what is sizeof(...)" questions, plus tricky ones on static counters and extern linkage. Getting these right signals you actually understand memory, not just syntax.
The C standard fixes only minimum sizes, not exact ones — actual sizes depend on the compiler/architecture. sizeof(char) is always 1 by definition (it is the unit of sizeof).
| Type | Typical size (64-bit) | Standard minimum |
|---|---|---|
| char | 1 byte | 1 byte (≥8 bits) |
| short | 2 bytes | ≥16 bits |
| int | 4 bytes | ≥16 bits |
| long | 8 bytes (Linux) / 4 (Windows) | ≥32 bits |
| long long | 8 bytes | ≥64 bits |
| float | 4 bytes | — |
| double | 8 bytes | — |
A storage class sets a variable's scope, lifetime, linkage, and default value.
| Class | Lifetime | Linkage | Default |
|---|---|---|---|
| auto | block | none | garbage |
| register | block | none | garbage |
| static | whole program | internal | 0 |
| extern | whole program | external | 0 |
sizeof(char) is always 1; every other size is platform-dependent, so never hard-code 4 for int — use sizeof.static local variable keeps its value between calls and is initialized only once; default value is 0.register is just a hint — you cannot take the address (&) of a register variable.extern declares but does not define — no memory is allocated; the definition lives in another file.signed/unsigned matters: an unsigned int can never be negative, so unsigned x = -1 wraps to the max value.int is always 4 bytes — on a 16-bit embedded target it may be 2 bytes; always verify with sizeof."What will sizeof print?", "difference between static and extern?", or a static-counter loop asking for its value across calls. Output-prediction on unsigned wraparound is a favourite.
sizeof(char)==1 always; standard fixes minimums only. Four storage classes: auto/register (block, garbage), static (persistent, 0, internal linkage), extern (declaration only, external linkage). Globals/statics default to 0, locals to garbage.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.