Compile C and Understand Errors Before Running
Compile C and Understand Errors Before Running
A teacher edits a program but the old executable still prints the old message. The latest compilation failed, so running the existing file did not test the edited source.
Compilation checks and translates source; linking resolves referenced functions; execution runs the resulting program. A successful build does not prove that the program computes the right answer.
Explain
Distinguish a compiler error, linker error and wrong computed result.
Trace
Add a third subject worth 20 marks and predict the new total.
Apply
Check a computed result even after compilation succeeds.
Compile and Debug: follow the data
Start
A teacher edits a program but the old executable still prints the old message. The latest compilation failed, so running the existing file did not test the edited source.
Operation
Compilation checks and translates source; linking resolves referenced functions; execution runs the resulting program. A successful build does not prove that the program computes the right answer.
Result
Total: 75
Understand the operation
A missing semicolon is usually diagnosed during translation. Calling a declared function whose definition was not linked can cause a linker error. A program that subtracts marks instead of adding them can build and run while producing a wrong total.
Use cc -std=c17 -Wall -Wextra -Wpedantic school.c -o school and read the first relevant diagnostic. Fix its cause before chasing errors caused by that earlier mistake. Run the newly built executable only after successful compilation.
Read the complete C program
This complete C17 program adds marks from two subjects. The declaration int maths = 40; creates a named whole-number value; int science = 35; creates another. The + sign adds them. In printf, %d is replaced by the integer result and \n starts a new line.
#include <stdio.h> provides the declaration of printf. Execution starts at main; return 0; reports successful completion. The next lessons explain these declarations in more detail.
C17 · complete program
#include <stdio.h>
int main(void) {
int maths = 40;
int science = 35;
printf("Total: %d\n", maths + science);
return 0;
}
Expected output
Total: 75
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int maths = 40;
int science = 35;
printf("Total: %d\n", maths + science);
return 0;
}
The compiler checks the integer declarations and printf call; the linker provides printf.
The executable calculates 40 + 35 and prints 75. Checking the value catches errors that a syntactically valid program can contain.
Follow the worked calculation
Read the starting values
The compiler checks the integer declarations and printf call; the linker provides printf.
Perform the operation
The executable calculates 40 + 35 and prints 75. Checking the value catches errors that a syntactically valid program can contain.
Check the observable result
Total: 75
Total: 75
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int maths = 40;
int science = 35;
printf("Total: %d\n", maths + science);
return 0;
}
Add a third subject worth 20 marks and predict the new total.
Worked answer
The third declared value participates in the sum, so 40 + 35 + 20 produces 95. Rebuild before comparing the screen with that prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int maths = 40;
int science = 35;
int english = 20;
printf("Total: %d\n", maths + science + english);
return 0;
}
Expected output
Total: 95
Run in C compiler →Recognize the important boundaries
Warnings are evidence to investigate; silence from a compiler is not a proof of memory safety. Later tests should cover empty inputs, limits and failure paths.
A local debugger can stop at a breakpoint and inspect variables. Sanitizers can find some invalid accesses; they do not prove that every possible execution is valid.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Compilation checks and translates source; linking resolves referenced functions; execution runs the resulting program. A successful build does not prove that the program computes the right answer.
Continue with testing boundary cases.
Toolchain references: GCC language standards and Clang compiler options.
Reaching the recap and completing the quizzes are separate milestones.