Data types & sizes
Why int is 4 bytes, what sizeof really asks, and integer overflow.
Before you start
A **type** is a promise about what kind of value lives in a variable and how many bytes of memory it takes up. int means "a whole number, 4 bytes". char means "one character, 1 byte".
Types matter because memory is finite. A single byte can only hold 256 different values. If you try to store 300 in it, something has to give — and what gives is called *overflow*, one of the oldest and nastiest bug categories in programming.
This lesson shows you the exact byte-count of every basic C type, and walks 127 + 1 wrapping around into -128 so you can see overflow with your own eyes.
Stack
each type occupies a fixed number of bytes1 byte = 8 bits = 2⁸ = 256 possible values.
Step 1 of 7. char: 1 byte, 256 values. A `char` occupies exactly 1 byte — the smallest addressable unit. It can hold values from -128 to 127 (signed) or 0 to 255 (unsigned). The number 127 fits comfortably. We will come back to see what happens when it does not.
Step 01 / 07
char: 1 byte, 256 values
A char occupies exactly 1 byte — the smallest addressable unit. It can hold values from -128 to 127 (signed) or 0 to 255 (unsigned).
The number 127 fits comfortably. We will come back to see what happens when it does not.
#include <stdio.h>#include <limits.h>int main(void) { char c = 127; short s = 32000; int i = 2147483647; long l = 100L; float f = 3.14f; double d = 3.14159265358979; printf("sizeof(char) = %zu\n", sizeof(char)); // 1 printf("sizeof(short) = %zu\n", sizeof(short)); // 2 printf("sizeof(int) = %zu\n", sizeof(int)); // 4 printf("sizeof(long) = %zu\n", sizeof(long)); // 8 printf("sizeof(float) = %zu\n", sizeof(float)); // 4 printf("sizeof(double) = %zu\n", sizeof(double)); // 8 c = c + 1; // 127 + 1 = ? return 0;}