MCS-201 · Unit 1

Programming Fundamentals

Section 9 of 9

Block 1 · Unit 1Programming Fundamentals20 min · Section 9 of 9

1.9Linking, Running and Debugging

Covers IGNOU MCS-201 §1.10, 1.10.1, 1.10.2, 1.10.3, 1.10.4, 1.11

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.

ExpressionOperand typesResult
9 / 4int, int2
9 % 4int, int1 (the remainder)
9.0 / 4real, int2.25
9 / 4.0int, real2.25
(float)9 / 4real (after the cast), int2.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

ErrorDetected byWhenExample
SyntaxCompilerCompile timeMissing semicolon
SemanticCompilerCompile timeString assigned to an int
LinkerLinkerLink timeFunction declared but never defined; misspelt main
RuntimeProcessor / operating systemWhile runningDivision by zero, missing file
LogicalNobody; only testingAfter runningWrong 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 1 · multiple choice

Why must a compiled C program be linked?

Show answer

Answer: C. To combine the object file with the library code of functions such as printf

The object file only calls printf; the linker adds its code from the library. Checking grammar and removing comments happen during compilation and preprocessing.

Question 2 · multiple choice

What value is stored in x after float x = 7 / 2;?

Show answer

Answer: B. 3.0

7 / 2 uses two integers, so it gives 3, which is then stored in the float as 3.0. Writing 7 / 2.0 would give 3.5.

Question 3 · multiple choice

What is the value of 9.0 / 4?

Show answer

Answer: C. 2.25

One operand is real, so C performs real division and keeps the fraction. Integer division (2) happens only when both operands are integers.

Question 4 · multiple choice

A program asks how many items were bought and divides the total price by that number. The user enters 0. What type of error is this?

Show answer

Answer: A. Runtime error

The method is correct, but during execution the program attempts an impossible operation for this input. A logical error would give wrong results even for valid inputs.

Question 5 · multiple choice

A program calculates the area of a circle as area = 2 * 3.14 * r;. What type of error is this?

Show answer

Answer: D. Logical error

2πr2\pi r is the circumference; the area is πr2\pi r^2. The code is valid C, so the compiler accepts it and only the output is wrong, which makes it a logical error.

Question 6 · multiple choice

A program compiles, but linking fails with “undefined reference to main”. What is the most likely cause?

Show answer

Answer: B. The main function is misspelt, for example mian or Main

The operating system needs a function named exactly main; a misspelt name compiles as an ordinary function, but the linker cannot find main. A missing semicolon would stop compilation earlier.

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.

↑ ↓ move↵ openTab switch type