What is undefined behavior in C?
Undefined behavior (UB) means the C standard gives no rule about what the program will do. It may work, crash, or give a wrong result, and it can change with the compiler or the options.
Common examples:
- Reading an uninitialized variable.
- Accessing an array outside its limits.
- Dereferencing a NULL or dangling pointer.
- Signed integer overflow, like
INT_MAX + 1. - Modifying a variable two times in one expression, like
i = i++ + ++i. - Dividing by zero.
The danger is that the program may look fine on your computer. Compile with -Wall -Wextra and use -fsanitize=undefined to catch many of them.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |