Skip to main content

When you learn a human language, you often will learn to both read and write that language. This is also true when learning programming. You don't just need to learn to write programs, you also need to learn to read it. However, reading programs isn't exactly the same as reading human language. We need to follow the program correctly and we need to know how to track our variables. This section explains how to do this. A walkthrough is a type of problem that involves tracing through a program and determing its exact output.

How to do a walkthrough

Video version of this example

Start by creating a table containing all the variables in the program. You will update the table for the corresponding variable each time a value is modified. Tracking each variable using a table will make it easy for you to always know what the current value is for each variable.

When you encounter source code lines with instructions to display output, follow the format string and supply the corresponding variable values from the table.

Example Walkthrough:

#include <stdio.h>

int main(void)
{
// Variables:
int a = 5;
int b = 10;
int c = a + b;

// Output!
printf("a = %d, b = %d, c = %d\n", a, b, c);

// Some calculations...
a = (a + 8) % 3;
b += (a + a + a);
c = b * 2 / 7

// Output!
printf("a = %d, b = %d, c = %d\n", a, b, c);

return 0;
}

To do the above source code walkthrough, start by making a table where each column represents a variable. In this case a, b and c:

abc

The first 3 lines of code in the main function assigns (=) a value for each variable:

	int a = 5;     // a stores the value 5
int b = 10; // b stores the value 10
int c = a + b; // c stores the value 15

As variables are modified, track the changes in the table:

abc
51015

The next line of execution is an instruction to produce output (to the screen):

	printf("a = %d, b = %d, c = %d\n", a, b, c);

Output statements are written down exactly how it would appear on the screen based on the formatting string (double quoted part). All % parts within the format string will be substituted with the corresponding variable value listed after the format string (comma delimited). Substitutions are done in sequence (first %d is replaced with the value of variable a, the second %d is replaced with the value of variable b and so on).

Based on the above output statement, the following would be written down:

a = 5, b = 10, c = 15

The next executable line is performing a calculation where the variable a will be updated with the result:

	a = (a + 8) % 3;

Expanding the expression after substituting the variable a (refer to the walkthrough table for the value of variable a) becomes:

	a = (5 + 8) % 3;

This expression will be evaluated as described in the following steps:

a = (5 + 8) % 3  // (5 + 8)results in 13
\ /
a = 13 % 3 // 13 % 3 results in 1
\ /
a = 1

Variable a is now assigned the value 1.

Update the table (cross out the old value and place the new value on the next line under the column for variable a):

abc
51015
1

The next executable line will update variable b:

	b += (a + a + a);

Substitute variable a with its value 1:

b += (1 + 1 + 1)
b += 3

NOTE: b += 3 is shorthand for b = b + 3 therefore, b = 10 + 3

Variable b has a current value of 10, and now 3 is being added to it, so the result of the above calculation modifies b to = 13.

It's time to update the table for variable b!

abc
51015
113

The last calculation updates variable c:

	c = b * 2 / 7

Substituting variable b with its value 13 (refer to the walkthrough table) yields the following evaluation:

c = 13 * 2 /7
c = 26/7
c = 3

WAIT! 26/7 = 3.71 so why is c=3?

Integer arithmetic is unable to store precision values (whole numbers only) so the decimal values are dropped (truncated). Integer division does not follow rounding rules

It's time to update the table for variable c!

abc
51015
1133

The next line of execution is an output statement:

	printf("a = %d, b = %d, c = %d\n", a, b, c);

Once again, substitute each % with the corresponding variable value (refer to the walkthrough table)! Written down what would be displayed on the screen:

a = 1, b = 13, c = 3

Assembling all the output results in sequence yields what the program would display on the computer screen if it were executed:

a = 5, b = 10, c = 15
a = 1, b = 13, c = 3