Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

undefined-behavior

Undefined behavior

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
advanced

This lesson

This lesson teaches Undefined behavior: the syntax, patterns, and safety habits you need before advancing in C.

Undefined behavior is why senior C reviews are strict—what “works on my machine” is not enough.

You will apply Undefined behavior in contexts like: Kernels, drivers, embedded devices, and performance libraries used by other languages.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true).

When pointers, structs, and basic control flow from intermediate lessons are familiar.

Undefined behavior (UB) means the standard places no requirements on what happens. The program may crash, appear to work, or behave differently after recompile—unlike defined errors in Java.

Common UB sources

  • Out-of-bounds array access
  • Signed integer overflow
  • Dereferencing invalid pointers
  • Data races without synchronization
  • Reading uninitialized automatic variables

Implementation-defined vs unspecified

Some behavior varies by platform but is constrained; UB has no guarantees—treat it as a bug always.

Important interview questions and answers

  1. Q: Why care about UB if it works?
    A: Optimizers assume UB never happens—"working" code can break under -O2 or on another compiler.
  2. Q: Rust vs C on UB?
    A: Rust safe code avoids most UB at compile time; C requires discipline and tools.

Self-check

  1. Is signed int overflow defined in C?
  2. What happens after free() if you dereference the pointer?

Pitfall: Code with UB may work in debug builds then break under -O2—treat sanitizer warnings and compiler diagnostics seriously, unlike forgiving dynamic languages.

Interview prep

Why UB matters if code works?

Optimizers assume UB never happens—working debug code can break in release builds or on other compilers.

Signed overflow?

Undefined in C—use unsigned types or check before arithmetic if wrap semantics are needed.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • UB example?
  • Why compilers optimize UB?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump