ITM 102

Structured Programming in C

TU BITM · Semester 1 · BITM curriculum and programme regulation

Requirement
required
Credits
3
Study Guide
10 units available

Chapter notes

Authoritative study guide across 10 syllabus units

Open a unit to read its study notes.

Unit 1: Introduction to Programming Language· 7 LHs· ~9 marks

Part 1: Core Concepts (2 Marks)

  • Structured Programming: A programming paradigm that enforces clean logical organization using three fundamental control structures: Sequence, Selection (if-else, switch), and Repetition (for, while, do-while), strictly avoiding unstructured jumps (goto).
  • Compilation Pipeline in C: The five-stage build sequence: Preprocessing (cpp expands macros/headers to .i), Compilation (gcc generates assembly .s), Assembly (as outputs relocatable object code .o), Linking (ld resolves external libraries into the final executable .exe), and Loading (OS loads binary into RAM).
  • Algorithm vs. Flowchart: An algorithm is a finite, unambiguous, step-by-step written procedure for solving a computational problem; a flowchart is the standardized graphical representation of that algorithm using geometric symbols.
  • Linker Error vs. Runtime Error: A linker error occurs when the linker cannot resolve external function symbols (e.g., missing main() or unresolved library symbol); a runtime error occurs during execution (e.g., division by zero, segmentation fault).

Part 2: Structural Frameworks (10 Marks)

Complete C Compilation and Execution Pipeline

Complete C Compilation and Execution Pipeline
Complete C Compilation and Execution Pipeline
  1. Preprocessor (cpp): Ingests raw source code (program.c), strips comments, expands header directives (#include), and substitutes symbolic macro definitions (#define) to produce the expanded source file (program.i).
  2. Compiler (gcc): Performs lexical analysis, syntax parsing, semantic verification, and code optimization, translating the high-level C code into assembly language instructions (program.s).
  3. Assembler (as): Converts mnemonic assembly code into platform-specific machine code, outputting relocatable binary object files (program.o or program.obj).
  4. Linker (ld): Resolves external cross-references, binds functions from standard libraries (such as libc.a or msvcrt.lib), and merges multiple object files into a single standalone executable binary (program.exe).
  5. Loader (Operating System): Allocates virtual address space, loads code and static data segments into physical RAM, initializes runtime stack and heap spaces, and passes execution control to the entry point main().

Part 3: Distinction Tables

Basis of Comparison Low-Level Language (Assembly) High-Level Language (C)
Hardware Dependency Strictly tied to specific CPU architecture & register set Platform-independent (portable source code)
Execution Speed Maximally fast; direct 1:1 hardware translation Marginally slower due to compiler abstraction
Programmer Productivity Extremely tedious; requires manual register allocation High productivity; expressive arithmetic syntax
Memory Abstraction Direct physical memory addresses and stack manipulation Abstract variable identifiers and type safety

Part 4: Worked Problems & Exam Traps

Algorithm and Compilable C Boilerplate

#include <stdio.h>

int main(void) {
    // 1. Sequence: Variable Declaration
    int n, sum = 0;

    printf("Enter a positive integer: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Error: Invalid positive integer input.\n");
        return 1;
    }

    // 2. Iteration: Loop Accumulation
    for (int i = 1; i <= n; i++) {
        sum += i;
    }

    // 3. Selection & Output
    printf("Sum of first %d natural numbers = %d\n", n, sum);
    return 0;
}

TU Exam Trap: Never confuse #include <stdio.h> with a pre-compiled library. The .h header contains only function prototypes and macro definitions; the actual compiled implementation is linked during the linking phase from libc.