See the idea.
Follow each step.
Explore 11 step-by-step models of C ideas. Open a model, choose a case, and follow the trace at your own pace. A complete text explanation accompanies every model.
See a value get copiedC17
See a value get copied
VISUAL MODEL
Change a starting value, then follow three lines. Watch the two objects separately.
Assume these declarations are inside a function.
int apples = 4;int saved = apples;apples = 6;
applesInitializer not reachedsavedInitializer not reachedNeither initializer has been reached. No starting value is shown for apples or saved in this trace yet.
The diagram shows logical objects and values. Physical storage and optimisation depend on the implementation.
Read the complete trace
- Neither initializer has been reached. No starting value is shown for apples or saved in this trace yet.
- The initializer gives apples the value 4. Its name and type stay the same when a later assignment changes its value.
- The initializer gives saved a copy of 4. The two integer objects hold separate values.
- apples now holds 6. saved still holds 4, the value copied earlier.
Follow two pointers and their targetsC17
Follow two pointers and their targets
VISUAL MODEL
Copy a pointer, redirect one copy, then write through each pointer. The arrows show which object each value refers to.
Start with int left = 4; and int right = 8; inside a function.
int *p = &left;int *q = p;p = &right;*q = 6;*p = 9;
pPoints toInitializer not reachedqPoints toInitializer not reachedleft4right8left and right are initialised. The initializers for pointer objects p and q have not been reached yet.
left and right remain alive and writable throughout. The arrows describe relationships, while actual pointer representations depend on the implementation.
Read the complete trace
- left and right are initialised. The initializers for pointer objects p and q have not been reached yet.
- p receives a pointer to left. This does not copy left into p; p holds a pointer value.
- q receives a copy of p’s pointer value. Both now refer to left.
- p now refers to right. q still refers to left because q keeps its own copied pointer value.
- Writing 6 through q changes left. q itself still refers to left.
- Writing 9 through p changes right. Trace the pointer’s current target before deciding which integer changes.
Follow recursive calls and returnsC17
Follow recursive calls and returns
VISUAL MODEL
Choose a small input and follow triangle(n). See which call waits and which result lets its caller continue.
triangle adds the unsigned integers from 1 through n. Each invocation has its own n parameter.
if (n == 0U) return 0U;return n + triangle(n - 1U);
No call is in progress.
Ready to call triangle(3). No call has started yet.
The input is limited to 0–5, and every sum fits unsigned int. This follows source-level calls; storage layout and call optimisation are implementation details.
Read the complete trace
- Ready to call triangle(3). No call has started yet.
- Enter triangle(3). This call has its own parameter value 3.
- Enter triangle(2). This call has its own parameter value 2.
- Enter triangle(1). This call has its own parameter value 1.
- Enter triangle(0). This call has its own parameter value 0.
- triangle(0) reaches the stopping case and returns 0. No smaller call is needed.
- triangle(0) returned 0. Its caller triangle(1) can now compute 1 + 0 and return 1.
- triangle(1) returned 1. Its caller triangle(2) can now compute 2 + 1 and return 3.
- triangle(2) returned 3. Its caller triangle(3) can now compute 3 + 3 and return 6.
- All calls have finished. triangle(3) returned 6 to the original caller.
Move through an arrayC17
Move through an array
VISUAL MODEL
Choose an index. See which element the pointer reaches, including the one-past boundary.
Inside a function, a contains three initialized int elements. Index 3 is a boundary, not a fourth element.
int a[3] = {3, 6, 9};int *p = a;p = a + 1;if (p != a + 3) { int value = *p; (void)value; }
a[0]3Array elementa[1]6Array elementa[2]9Array elementa + 3—One-past; do not read- p refers to
- —
- Value read
- —
a has three elements: 3, 6, and 9. The pointer initializer has not been reached.
Pointer steps count elements, not a fixed number of bytes. A one-past pointer may be formed and compared here, but not dereferenced. No out-of-bounds read is executed.
Read the complete trace
- a has three elements: 3, 6, and 9. The pointer initializer has not been reached.
- In this initializer, a converts to a pointer to its first element. p points to a[0].
- p receives a + 1. Adding an index moves by that many int elements.
- This pointer refers to an element. Reading *p yields 6. a[1] names the same element.
Find where a string endsC17
Find where a string ends
VISUAL MODEL
Keep the array size fixed and move the first null character. Watch the string length change.
These fragments are inside a function with <string.h> included. Both choices use three basic execution characters.
char text[6] = "cat";size_t before = strlen(text);text[1] = '\0';size_t after = strlen(text);
text[0]—Initializer not reachedtext[1]—Initializer not reachedtext[2]—Initializer not reachedtext[3]—Initializer not reachedtext[4]—Initializer not reachedtext[5]—Initializer not reached- Array capacity
- 6
- strlen result in this step
- —
The initializer has not been reached. The declared capacity is six char elements.
Capacity counts all array elements. strlen counts characters before the first null character, excluding that terminator. Later bytes are not erased by an earlier terminator.
Read the complete trace
- The initializer has not been reached. The declared capacity is six char elements.
- The literal initializes three letters and a null. The remaining two elements are also zero-initialized.
- strlen(text) is 3. Capacity remains 6. The first null is at index 3.
- Writing a null at index 1 creates an earlier terminator. The later elements keep their stored characters.
- strlen(text) is now 1. The array still has six elements; length and capacity are different facts.
Follow a function argumentC17
Follow a function argument
VISUAL MODEL
Watch a parameter receive a value, then change without changing the caller’s object.
replace is a separate function. The final two lines belong inside another function that calls it.
void replace(int x) {x = 9;}/* inside a different function: */int caller = 4;replace(caller);
caller4Caller’s objectx—No active parametercaller is initialized to 4 in the calling function. No replace call is active yet.
C passes argument values. A pointer argument is copied too; that copied pointer can still refer to an object shared with the caller. This model passes an int.
Read the complete trace
- caller is initialized to 4 in the calling function. No replace call is active yet.
- The argument expression caller supplies its current value, 4.
- The parameter x is a separate int object initialized from the argument value 4.
- x receives 9. caller still holds 4 because it is a different object.
- replace returns. The parameter’s lifetime ends, and caller still holds 4.
Separate visibility from lifetimeC17
Separate visibility from lifetime
VISUAL MODEL
Follow an inner block containing an ordinary local and a static local. Track names and objects separately.
The inner block is inside a running function. local is a non-VLA automatic int. kept has static storage duration and is initialized before program startup.
/* inside a function, before the inner block */{static int kept = 2;int local = 3;/* both names are visible here */}/* outside the inner block */
kept2Alive; name out of scopelocal—Lifetime not begun; name hiddenBefore entering the inner block, kept is alive but its name is not in scope here. local’s lifetime has not begun.
Scope answers where a name is visible. Lifetime answers when an object exists. An ordinary non-VLA automatic object’s lifetime begins at block entry, before its initializer is reached.
Read the complete trace
- Before entering the inner block, kept is alive but its name is not in scope here. local’s lifetime has not begun.
- Entering the block begins local’s lifetime. Its name has not been declared here yet, and its initializer has not run.
- kept’s declaration brings its name into scope. It already holds 2; entering the block does not reinitialize it.
- local’s declaration makes the name visible, and its initializer stores 3. Both objects are alive.
- Here both names are visible and both objects are alive. These are separate properties.
- Leaving the block ends local’s lifetime. kept remains alive, but both names are outside their scope here.
- kept still exists with value 2. This model describes its state; code here cannot use that out-of-scope name directly.
Resize and release an allocationC17
Resize and release an allocation
VISUAL MODEL
Explore both outcomes of a positive-size realloc request. Follow the owning pointer and the temporary result.
Include <stdlib.h> before the function definition. Run this fragment inside a void function. Assume the first malloc succeeds. The realloc outcome is a teaching scenario, not a real allocator control.
int *p = malloc(2 * sizeof *p);if (p == NULL) return;p[0] = 4; p[1] = 8;int *tmp = realloc(p, 3 * sizeof *p);if (tmp != NULL) {p = tmp;p[2] = 12;}free(p);p = NULL; tmp = NULL;
p—Initializer not reachedtmp—Initializer not reachedB (allocation)—No allocation yetNo allocation has been requested yet. Neither pointer initializer has been reached.
A and B are logical allocation labels, not addresses. ? means a value must not be read. Successful realloc ends the old allocation’s lifetime even if an implementation reuses an address. Only positive sizes are used.
Read the complete trace
- No allocation has been requested yet. Neither pointer initializer has been reached.
- malloc succeeds and p owns allocation A, with room for two int objects. Their values have not been initialized.
- The two int values become 4 and 8. The requested allocation size was expressed using sizeof *p.
- realloc succeeds. tmp refers to allocation B; the first two values are preserved. The old allocation has ended, so the old value in p must not be read.
- p receives tmp, replacing its unusable old value without reading it. p now owns B; tmp is an alias.
- p[2] receives 12. All three int values in B are now initialized.
- free releases the live allocation once. Pointer values that referred to it must not be read afterward. No value is read from released storage.
- Both pointer objects are assigned NULL. This replaces old values; it does not release any additional allocation.
Follow source into a programC17
Follow source into a program
VISUAL MODEL
Connect headers, macro replacement, translation units, and external definitions. Try leaving out one definition.
The labelled excerpts belong to config.h, main.c, and scale.c. The definition choice controls whether scale.c is supplied to the build.
/* config.h */ #define FACTOR 2int scale(int);/* main.c */ #include "config.h"int main(void) { return scale(3); }/* scale.c */ #include "config.h"int scale(int n) { return n * FACTOR; }
source—Current stagepreprocess—Later stagecompile—Later stagelink—Later stageprogram—Later stageconfig.h declares scale and defines FACTOR as 2. main.c calls scale; scale.c supplies its definition when included in this build.
This is a conceptual grouping of translation phases. Separate .o files and compiler/linker executables are common toolchain choices, not required C artifacts. These tiny files need no standard library function.
Read the complete trace
- config.h declares scale and defines FACTOR as 2. main.c calls scale; scale.c supplies its definition when included in this build.
- Each source file is preprocessed separately. The header supplies declarations, and FACTOR tokens in scale.c become 2.
- The resulting translation units are analyzed and translated. A declaration lets main.c be translated without placing scale’s body in main.c.
- The external call is connected with scale’s definition from the other translation unit.
- The linked program can run. scale(3) computes 6, and main returns that value.
Follow the type before the resultC17
Follow the type before the result
VISUAL MODEL
Separate the operands, the operation, and conversion to the destination. These are reasoning stages, not separate machine instructions.
Each case is a fragment inside a function. The unsigned-char case specifically assumes CHAR_BIT == 8; <limits.h> supplies that macro.
int numerator = 5, denominator = 2;double ratio = numerator / denominator;
numerator5Operand valuedenominator2Operand valuenumerator / denominator—Not produced yetratio—Not produced yetBoth operands are int. ratio will have type double, but that does not change the division’s operand types.
A cast does not check a range. The original objects are not changed by operand conversion. C does not require 8-bit bytes; only the labeled unsigned-char case assumes them. Signed overflow remains undefined, including in C23.
Read the complete trace
- Both operands are int. ratio will have type double, but that does not change the division’s operand types.
- 5 / 2 is integer division, so the quotient is the int value 2. This card represents the expression result, not a changed declaration of ratio.
- The int quotient 2 converts to double and initializes ratio with 2.0. The fraction was already discarded.
Macro expansion happens before expression evaluationC17
Macro expansion happens before expression evaluation
VISUAL MODEL
Follow the argument tokens into two replacement lists. Then evaluate the resulting C expression using its grouping.
These examples use only the side-effect-free int expressions 3 and 2 + 1. The macro definition precedes the fragment inside a function.
#define SQUARE(x) ((x) * (x))int value = SQUARE(2 + 1);
argument2 + 1Preprocessing tokensreplacement—Not available yetvalue—Not available yetRead the replacement list. Its two occurrences of x will each receive the argument’s tokens. No parameter object is created.
Parentheses repair this grouping problem; they do not make a repeated argument evaluate just once. A macro is not a function call. This model never runs SQUARE(i++) or other unsequenced modifications.
Read the complete trace
- Read the replacement list. Its two occurrences of x will each receive the argument’s tokens. No parameter object is created.
- Substitute 2 + 1 for each x. The resulting expression is ((2 + 1) * (2 + 1)). This is preprocessing, not execution of multiplication.
- At execution, the resulting expression produces 9. Parentheses decide the grouping; they do not add a function-call boundary.