A compiled object file is not yet a complete program: it calls library functions whose code it does not contain. Linking completes it, and running it reveals the errors that no tool can catch in advance. This section covers the last stages of the journey and the three kinds of errors that appear there.
Why linking is needed
Your object file calls functions such as printf but does not contain their code. That code is already compiled and stored in the C standard library.
The linker produces an executable file. On Windows it has the extension .exe; on UNIX, Linux and macOS no extension is needed (the default name is a.out). Commands such as clang hello.c -o hello compile and link in one step.
hello.csource file
→
Compilersyntax and semantic errors stop here
→
hello.oobject file
→
Linker + librarylinker errors stop here
→
helloexecutable
→
Runruntime and logical errors appear here
From source file to running program
A header file such as stdio.h contains only declarations: it tells the compiler that a function called printf exists and what arguments it takes. The compiled code of printf lives in a library file. The compiler is satisfied by the declaration; the linker then has to find the code.
Errors that appear when the program runs
After compiling and linking, running the program has three possible outcomes: correct results, wrong results (a logical error), or the program fails during execution (a runtime error).
Logical errors and integer division
A logical error is a mistake in the method: the program runs normally but gives wrong results. Neither the compiler nor the linker can detect it, because the code is valid. Logical errors are found by testing and debugging.
A very common logical error in C comes from integer division.
Expression
Operand types
Result
9 / 4
int, int
2
9 % 4
int, int
1 (the remainder)
9.0 / 4
real, int
2.25
9 / 4.0
int, real
2.25
(float)9 / 4
real (after the cast), int
2.25
Runtime errors
A runtime error occurs while the program is running, when it attempts an operation that cannot be carried out, such as dividing by zero, using memory it does not own, or opening a file that does not exist. Runtime errors often depend on the input: the same program can work for one value and fail for another.
Debugging
Debugging is the process of finding and removing errors from a program. Useful methods:
Trace the program by hand with a small input, writing down each variable’s value.
Add temporary printf statements to display intermediate values.
Use a debugger such as lldb (macOS) or gdb (Linux) to pause the program and inspect variables.
Test with edge cases: zero, negative numbers, very large values, empty input.
Summary of error types and the execution process
Error
Detected by
When
Example
Syntax
Compiler
Compile time
Missing semicolon
Semantic
Compiler
Compile time
String assigned to an int
Linker
Linker
Link time
Function declared but never defined; misspelt main
Runtime
Processor / operating system
While running
Division by zero, missing file
Logical
Nobody; only testing
After running
Wrong formula; integer division
The study material’s program execution process (Figure 1.6) summarises the cycle: write the program → compile it → fix syntax errors, save and recompile → link → execute → test and verify the results.
Key takeaways
The linker joins object files and library code into an executable (.exe on Windows; no extension needed on UNIX and macOS).
Headers declare functions; libraries contain their code.
Runtime errors happen during execution and may depend on input; in C some, such as integer division by zero, are undefined behaviour.
Logical errors produce wrong results and are found only by testing and debugging.
Integer division truncates; make an operand real to keep the fraction.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 7 · short answer
What is stored in y after float y = 1 / 3;? Explain why, and rewrite the line so that y holds approximately 0.333333.
Show worked answer
y holds 0.0. Both operands, 1 and 3, are integers, so C performs integer division: 1 ÷ 3 = 0 with the fraction discarded. Only then is the 0 converted to float and stored as 0.0. To keep the fraction, make at least one operand real, for example float y = 1 / 3.0; or float y = (float)1 / 3;, which stores approximately 0.333333.
Question 8 · short answer
Name the error type in each case: (a) a missing semicolon; (b) a function declared and called but never defined; (c) a program prints 11 as the average of 10, 5 and 20; (d) a program stops when the user enters 0 as a divisor.
Show worked answer
(a) Syntax error, found by the compiler because a grammar rule is broken. (b) Linker error, because the code compiles but the linker cannot find the function’s definition. (c) Logical error, because the program runs but integer division produces the wrong average (the correct value is about 11.67). (d) Runtime error, because the method is correct but an impossible operation is attempted during execution for that input.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/programming-fundamentals/linking-running-and-debugging/ to read this page.