In C, some mistakes do not give an error. They give no promises.
Direct answer
Undefined behavior (UB) occurs when your code does something the C standard does not define, so anything can happen. It may work on your machine and fail on another. Avoid it by initialising variables, staying in bounds, checking pointers and testing with sanitizers.
Common examples
int x;
printf("%d\n", x); // uninitialised
int a = 2147483647;
a = a + 1; // signed overflow
int arr[3];
arr[3] = 1; // out of bounds
int *p = NULL;
*p = 1; // null dereference
How to find it
gcc -Wall -Wextra -g -fsanitize=address,undefined main.c -o app
./app
Habits that help
- Initialise everything.
- Check array indexes.
- Check pointers.
- Compile with warnings and sanitizers.
Where Teyro fits
Teyro's C for Beginners course turns this into short lessons you can play in a few minutes a day. It has 78 lessons, and you can start free.
The bottom line
"It works on my machine" is not enough in C. Prevent UB by design.



