🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Tracing C Function Calls
Learn a reliable paper method for tracing C function calls, including type conversion of arguments, unsafe evaluation order, loops inside callees and mutual recursion.
Reading code and knowing its output is a core engineering skill, and tracing C function calls by hand is how you build it. Debuggers help, but on a tiny microcontroller with no debug probe attached, or in a written interview, you must simulate the program in your head. This lecture of our free C programming course gives you a step-by-step method and applies it to four situations that trip people up: arguments that get converted to a different type, arguments whose evaluation order is not defined, loops whose counters are modified in surprising ways, and two functions that call each other. It is written as a free C tutorial for anyone aiming at embedded C programming jobs.
Every program here was compiled and executed with GCC, and every output shown is the real output.
What You Will Learn
- A four-column trace table method for tracing C function calls
- How a prototype converts arguments, and how floating-point values are truncated
- Why expressions such as two increments of one variable in one call are unsafe
- How to trace a for loop whose body also changes the loop counter
- How mutual recursion works and why it needs a forward declaration
- How to follow the call stack of two cooperating functions
Prerequisites
You should already know pass by value, scope, the return statement and the basic for loop. Earlier lectures in this free embedded C course cover these topics, and the recursion lectures of this chapter explain the call stack in more depth.
A Method for Tracing C Function Calls
Do not try to hold the whole program in your head. Draw a small table with one column per variable that matters and one row per step. Every time a function is called, start a new table for its parameters and local variables. Every time it returns, cross that table out and write the returned value next to the call in the caller’s table.
Argument Type Conversion When Tracing C Function Calls
With a prototype in scope, each argument is converted to the type of its parameter. Converting a floating-point value to int discards the fractional part toward zero. It does not round.
#include <stdio.h>
void show(int a, int b)
{
printf("%d %d\n", a, b);
}
int main(void)
{
show(7, 290.5);
show(-3.9, 'A');
return 0;
}
$ gcc -Wall -Wextra -std=c17 conv.c -o conv
$ ./conv
7 290
-3 65
The value 290.5 becomes 290, and -3.9 becomes -3, since truncation moves toward zero. The character constant 'A' is already an integer with value 65 in C, so it passes unchanged. If you compile with -Wconversion the compiler will point out each lossy conversion.
| Argument | Parameter type | Value received | Reason |
|---|---|---|---|
| 290.5 | int | 290 | Fraction discarded |
| -3.9 | int | -3 | Truncation toward zero |
| ‘A’ | int | 65 | Character code |
| 8 / 3 | int | 2 | Integer division |
Unsafe Evaluation Order in Function Arguments
C does not say in which order the arguments of a call are evaluated. That is harmless when the arguments are independent, but disastrous when two of them modify the same variable.
int i = 5;
printf("%d %d\n", i++, i++);
order.c:5:29: warning: operation on 'i' may be undefined [-Wsequence-point]
The standard calls this undefined behavior, so there is no correct answer to give. Different compilers, optimization levels, or even different targets can print different results. In an exam, the right response to such a program is to say that the result is unspecified or undefined, not to invent a number. In real code, split the work into separate statements:
int first = i++;
int second = i++;
printf("%d %d\n", first, second);
x and x++ (or --x) in one call.Tracing C Function Calls That Contain Loops
A function containing a loop runs the entire loop on every call. The loop counter is a local variable, so each call starts it fresh, and anything that changes it inside the body affects the loop’s progress.
void row(void)
{
int j;
for (j = 1; j < 6; j++)
printf("%d ", ++j);
printf("\n");
}
int main(void)
{
int i;
for (i = 1; i < 4; i++)
row();
return 0;
}
Trace one call to row. Start with j = 1, which satisfies j < 6. In the body, ++j raises j to 2 and prints 2. The loop’s update step j++ makes it 3. Test again: 3 is less than 6, so the body increments to 4 and prints 4, then the update makes it 5. The body increments to 6 and prints 6, then the update makes it 7, and the test fails. Each call therefore prints 2, 4 and 6. The outer loop runs three times, so the output is the same line three times.
2 4 6
2 4 6
2 4 6
Two lessons follow. The counter i in main and the counter j in row are unrelated variables. And whenever a loop body changes its own counter, trace every iteration individually instead of guessing.
Mutual Recursion and Forward Declarations
Mutual recursion happens when function A calls function B and B calls A. Because the compiler reads the file from top to bottom, whichever function appears first would call a name it has not seen. A forward declaration, which is just a prototype, solves that.
int is_even(int n);
int is_odd(int n);
int is_even(int n) { return n == 0 ? 1 : is_odd(n - 1); }
int is_odd(int n) { return n == 0 ? 0 : is_even(n - 1); }
The pair works like this. Zero is even, so is_even(0) answers 1. Any other number is even exactly when the number before it is odd, so the question is handed to the sibling function with a smaller value. Eventually the value reaches zero and the chain stops.
void ping(int n);
void pong(int n);
void ping(int n)
{
if (n <= 0) return;
printf("ping %d\n", n);
pong(n - 1);
}
void pong(int n)
{
if (n <= 0) return;
printf("pong %d\n", n);
ping(n - 1);
}
$ ./mutual
1 0
ping 4
pong 3
ping 2
pong 1
The first line is the result of is_even(6) and is_odd(6). The remaining lines show the two functions taking turns while the argument shrinks.
A trace with a conditional on both sides
Now study a pair where each function prints first and decides to continue only while the value is positive. Function a prints n and, if n is positive, calls b with n minus 2. Function b prints n and, if n is positive, calls a with n minus 1. Starting with a(5): a prints 5 and calls b(3). Function b prints 3 and calls a(2). Function a prints 2 and calls b(0). Function b prints 0, but 0 is not positive, so the chain stops. The output is 5 3 2 0, which matches the real run.
Real-World Use of Mutual Recursion and State Machines
In embedded firmware, mutual calls often appear in parsers and protocol decoders, where the function handling a header calls the one handling a payload, which may call back to handle a nested header. Stack depth is the big risk. Each active call consumes stack memory, and a microcontroller may have only a few kilobytes. Prefer an explicit state variable and a loop in code that must run forever or handle untrusted input.
Common Mistakes and Troubleshooting
Tracing C function calls goes wrong in a handful of predictable ways.
| Mistake | Consequence | Fix |
|---|---|---|
| Assuming floats round when converted to int | Off-by-one results | Use round() explicitly when you want rounding |
| Modifying one variable twice in one call | Undefined behavior | Split into separate statements |
| Mixing up the caller’s and callee’s loop counters | Wrong trace | Keep a separate table per function |
| Missing forward declaration in mutual recursion | Implicit declaration error | Prototype both functions first |
| No base case in a recursive pair | Stack overflow | Ensure the argument always moves toward a stop condition |
Best Practices, Performance and Security
- Keep argument expressions simple, with no side effects.
- Practice tracing C function calls on paper regularly until the table becomes second nature.
- Compile with
-Wall -Wextra -Wconversionso sequence-point problems and lossy conversions are reported. - Performance: each recursive call pushes a frame, so loops are usually faster and use constant memory.
- Security: unbounded recursion driven by external input can be exploited to exhaust the stack, so always bound the depth.
- Document the base case of every recursive function in a comment.
Key Takeaways
- Trace with one table per active function call and cross it out on return.
- Arguments are converted to parameter types; float to int truncates toward zero.
- Two changes to the same variable inside one call produce undefined behavior.
- Each call to a function restarts its local loop counters.
- Mutual recursion needs prototypes, a base case and a watch on stack depth.
Conclusion
Skilled programmers are not faster at guessing outputs; they are more disciplined about writing steps down. The trace table works for every example in this lecture, and tracing C function calls with it scales to far bigger programs. Together with the previous lectures on matching arguments, pass by value and return statements, you now have a complete toolkit for answering function questions. The next and final file of this series is a practice lab containing a quiz and interview questions so that you can test everything you have learned in this free embedded C course.
Frequently Asked Questions
Does converting a double to int round the value?
No. It truncates toward zero, so 290.5 becomes 290 and -3.9 becomes -3.
In what order are function arguments evaluated?
The order is unspecified. Never rely on it when arguments have side effects.
Why does printf(“%d %d”, i++, i++) give different results on different compilers?
The behavior is undefined because the same variable is modified twice without a sequence point.
Do two functions need forward declarations to call each other?
At least one of them does, because the compiler must know the other function’s signature before the first call. Declaring both is clearest.
Does a for loop counter inside a function keep its value between calls?
No. An ordinary local variable is recreated on every call.
How deep can mutual recursion go on a microcontroller?
That depends on available stack, often only a few kilobytes. Estimate the frame size and bound the depth.
One More Step: Practice Lab
Finish this part of the free C programming course with a quiz and interview questions for embedded C programming.

2 Comments