C Debugging
Trace a total and count separately, then check a corrected mean with a small reproducible input.
How it works
Debugging starts with an observed mismatch and a small input that reproduces it. Check assumptions about types, ranges and control flow before rewriting unrelated code. Build with debug information and low optimisation when stepping through a program; compiler flags depend on your toolchain. Set a breakpoint before the wrong value is produced, inspect state and advance one operation at a time. This complete fixed program shows the intermediate facts that explain its result rather than including a broken operation as an executable example.
Run it locally
Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.
cc -std=c17 -Wall -Wextra -Wpedantic -Werror debugging.c -o lessonOn Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.
Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.
Example
#include <stdio.h>
int main(void) {
int readings[] = {10, 11, 12};
int total = 0;
size_t count = sizeof readings / sizeof readings[0];
for (size_t i = 0; i < count; ++i) { total += readings[i]; }
if (count == 0) { return 1; }
double mean = (double)total / (double)count;
printf("total=%d count=%zu mean=%.2f\n", total, count, mean);
return 0;
}
Expected result
total=33 count=3 mean=11.00
The trace makes it possible to distinguish a bad total from a bad divisor. A breakpoint after the loop should show total thirty-three and count three. When investigating memory errors, an AddressSanitizer-capable toolchain can add runtime checks; support varies, and enabling it is not equivalent to proving the absence of undefined behaviour. The course review reports the instrumentation it actually used.
For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.