Introduction to Computer Systems, Programming Languages, and C Fundamentals

Fundamentals of Computer Systems

  • Definition of a Computer System: An electronic device that performs operations by accepting data as input, storing data, manipulating or processing data according to set instructions, and producing results as output in a human-understandable format.

  • Core Operations Performed by a Computer:

    • Accepts input data from peripheral devices or files.

    • Processes or manipulates the data using a Central Processing Unit (CPU\text{CPU}).

    • Displays or stores the results in human-understandable output formats.

    • Stores data, instructions, and computation results in primary and secondary memory.

  • Primary Categorization of Computer Components:

    • Hardware: Physical electronic, electromechanical, and electromagnetic devices.

    • Software: Set of programs, commands, and data instructions that direct the hardware.

Computer System Architecture

Computer Hardware and Software

  • Computer Hardware:

    • Consists of the bare machinery, physical electronic components, electro-mechanical parts, and electromagnetic devices connected via interfacing data/address buses.

    • Core hardware components include:

    • Central Processing Unit (CPU\text{CPU}) & Fan: Performs core processing and arithmetic operations.

    • Mainboard / Motherboard: Connects all internal electronic components and buses.

    • Random Access Memory (RAM\text{RAM}): Volatile primary storage for running programs.

    • Hard Disk Drive (HDD\text{HDD}): Non-volatile secondary storage for data and software.

    • Power Supply Unit (PSU\text{PSU}): Supplies regulated electrical power to internal hardware.

    • VGA Card / Sound Card: Expansion cards for graphic and audio processing.

    • Optical Drive (DVD-ROM\text{DVD-ROM}) & Internal All-in-One Reader: Media input drives.

    • Peripherals: Monitor, Keyboard, Mouse, and Speakers.

Computer Hardware Overview
  • Computer Software:

    • Consists of instructions, code commands, computer programs, and structured data held in memory storage that direct hardware operations.

    • Analogy: Hardware represents the physical body of a computer system, whereas software represents its soul. Both are indispensable for operation.

Computer Software Overview
  • Categories of Software:

    • Application Software: Software developed to assist users in executing end-user tasks benefiting from computation. Subcategories include:

    • Business software and accounting packages (e.g., Tally/ERP 9, QuickBooks).

    • Computer-Aided Design (CAD\text{CAD}) software (e.g., SolidWorks).

    • Database Management Systems (DBMS\text{DBMS}).

    • Web browsers (e.g., Google Chrome, Safari, Microsoft Edge).

    • Media players (e.g., VLC Media Player) and graphics editing tools (e.g., Adobe Photoshop, Illustrator).

    • Educational, medical (EHR\text{EHR} systems), and decision-making applications.

Application Software Categorization
  • System & Programming Software: Specialized programs used by software engineers to write, edit, test, debug, and build other programs. Examples include:

    • Compilers and Interpreters (e.g., GCC compiler).

    • Integrated Development Environments (IDEs\text{IDEs}) such as Visual Studio, Code::Blocks, and Eclipse.

    • Operating Systems (e.g., Microsoft Windows, macOS, Linux).

    • Utility software (e.g., AVG Antivirus, Disk Cleanup, File Compression software, Backup utilities).

Programming Languages and Classification

  • Definition: Computer programming languages are symbolic instruction sets used by developers to create software, applications, operating systems, and web services.

  • Three Primary Generations/Types of Programming Languages:

    • 1. Machine Language (Lowest-Level):

    • Composed entirely of binary digits (00 and 11).

    • Symbol 00 represents the absence of an electrical pulse; symbol 11 represents the presence of an electrical pulse.

    • Directly understood and executed by the computer hardware without requiring translation.

    • Provides the fastest execution speed and optimal memory efficiency.

    • Highly complex for humans to read, write, or debug.

    • 2. Assembly Language (Low-Level):

    • Replaces binary codes with human-readable alphanumeric symbols called mnemonic codes (e.g., MOV, ADD, SUB, START, LABEL).

    • Mnemonic codes typically consist of up to 55 letters.

    • Also referred to as Symbolic Programming Language.

    • Requires a specialized language translator called an assembler to convert mnemonics into binary machine code.

    • Easier to debug and modify than machine language, but requires extensive hardware knowledge.

Low-Level Languages Diagram
  • 3. High-Level Languages (HLL):

    • Designed using English words and mathematical symbols to simplify program creation.

    • Does not require hardware mnemonic knowledge.

    • Follows a one-to-many translation ratio: a single high-level statement generates multiple low-level machine instructions.

    • User-friendly, easy to learn, maintain, and port across platforms.

    • Common High-Level Languages and Applications:

      • C: System programming, embedded systems, kernel drivers.

      • C++: Game engines, enterprise software, high-performance computing.

      • Java: Android application development, cross-platform enterprise software.

      • Python: Artificial Intelligence (AI\text{AI}), Machine Learning (ML\text{ML}), data science, scripting, automation.

      • JavaScript: Web application development (frontend and backend).

High-Level Languages Diagram

System Software and Language Translators

  • Language Translator: System software that converts programs written in high-level or assembly languages (source code) into executable machine code (target language in binary 00s and 11s).

  • Primary Translator Types:

    • Compiler:

    • System software that scans and translates the entire high-level source program at once into binary object code.

    • Detects syntax errors (grammatical violations) and semantic errors.

    • Generates separate object files (.o or .obj).

    • Example: GCC compiler for C/C++.

    • Assembler:

    • System software that translates symbolic assembly language mnemonics into machine code instructions.

    • Interpreter:

    • System software that reads, translates, and executes high-level source code line-by-line (statement-by-statement).

    • Executes code directly without saving a separate machine object code file.

    • Advantages: Simplifies incremental testing and debugging during development.

    • Disadvantages: Slower program execution speed due to real-time line-by-line translation.

    • Examples: Python, JavaScript, Ruby interpreters.

  • Comparative Summary of Translators:

    • Compiler: Input = High-Level Language; Output = Machine/Object Code; Translation Unit = Whole program simultaneously.

    • Interpreter: Input = High-Level Language; Output = Direct instruction execution; Translation Unit = Line/statement by statement.

    • Assembler: Input = Assembly Language; Output = Machine Code; Translation Unit = Instruction by instruction.

Program Development Lifecycle and Problem-Solving Tools

  • Stages of Developing a C Program:

    1. Algorithm: Defining logical step-by-step computational steps.

    2. Flowchart: Creating a diagrammatic/graphical representation of algorithm logic.

    3. Pseudocode: Formulating informal English-like algorithmic logic.

    4. Program: Writing actual syntax-compliant source code in C.

Stages of Developing a C Program
  • Algorithms:

    • A finite set of ordered, unambiguous step-by-step instructions to solve a defined computational problem.

    • Example 1: Find Average of Three Numbers:

    • Step 0: Start

    • Step 1: Input first number into variable AA

    • Step 2: Input second number into variable BB

    • Step 3: Input third number into variable CC

    • Step 4: Compute Sum=A+B+CSum = A + B + C

    • Step 5: Compute Avg=Sum3Avg = \frac{Sum}{3}

    • Step 6: Display AvgAvg

    • Step 7: End

    • Example 2: Find Maximum of Two Numbers:

    • Step 0: Start

    • Step 1: Input variable AA

    • Step 2: Input variable BB

    • Step 3: If A>BA > B then Max=AMax = A else Max=BMax = B

    • Step 4: Display MaxMax

    • Step 5: End

    • Example 3: Sum of First NN Natural Numbers:

    • Step 0: Start

    • Step 1: Input NN

    • Step 2: Set I=1I = 1

    • Step 3: Set Sum=0Sum = 0

    • Step 4: Repeat while INI \le N:

      • (a) Sum=Sum+ISum = Sum + I

      • (b) I=I+1I = I + 1

    • Step 5: Display SumSum

    • Step 6: End

  • Flowcharts:

    • Graphical representation of an algorithm using standard ANSI symbols connected by directional flow lines.

    • Standard Flowchart Symbols:

    • Oval (Terminator): Represents start or end of a flowchart.

    • Rectangle (Process): Represents calculation or data processing step.

    • Parallelogram (Input/Output): Represents reading input or displaying output.

    • Diamond (Decision): Represents conditional evaluation requiring a Yes/No or True/False branch.

    • Arrow (Flow Line): Displays execution flow direction (top-to-bottom, left-to-right).

    • Circle (On-page Connector): Connects flow lines on the same page.

    • Off-page Connector: Connects flow lines across different pages.

Algorithm Representation Using Flowcharts
  • Pseudocode:

    • An informal, English-like description of program logic lacking strict syntax syntax constraints.

    • Common keywords: BEGIN/END, READ/INPUT, DISPLAY/PRINT, IF-ELSE, WHILE/FOR.

    • Example: Find Maximum of Two Numbers in Pseudocode: text BEGIN INPUT A INPUT B IF A > B THEN Max <- A ELSE Max <- B END IF OUTPUT Max END &nbsp;&nbsp;&nbsp;&nbsp;

  • Comparison: Algorithm vs Flowchart vs Pseudocode:

    • Algorithm: Step-by-step textual solution; easy to modify; less time required.

    • Flowchart: Graphical diagram; very easy to understand; difficult to modify; more time required.

    • Pseudocode: Structured English-like text; very easy to modify; minimal execution setup time.

History and Overview of the C Language

  • Origins of C:

    • Developed in the early 1970s by Dennis Ritchie at Bell Laboratories (AT&T).

    • Created as an enhanced successor to the B programming language.

    • Standardized by the American National Standards Institute (ANSI) in 1989 (known as ANSI C or C89), and subsequently by ISO.

  • Definition of Programming: Systematic development of instructions directing a computer to accept input, process data according to logic, and yield output.

  • Core Characteristics and Importance of C:

    • Foundational structured programming language.

    • Provides raw hardware access and manual memory management capability.

    • Delivers high performance and fast execution speed.

    • Influenced modern successor languages including C++, Java, C#, and Python.

Basic Structure of a C Program

  • Standard Structural Sections of a C Source File:

    1. Documentation Section: Program descriptions written inside comments (/* multi-line */ or // single-line).

    2. Preprocessor Directive Section: Header inclusions (#include <stdio.h>) and macro declarations (#define).

    3. Global Declaration Section: Program-wide global variables and function prototypes.

    4. main() Function: Standard entry point required for execution.

    5. User-Defined Subroutines/Functions: Supplementary custom function code.

  • Basic C Program Example: ```c

    include // Preprocessor directive

    int main() // Entry point function { int a = 5, b = 3, sum; // Variable declaration sum = a + b; // Executable statement printf("%d", sum); // Output statement return 0; // Function return statement }   ```

C Preprocessor Directives and Conditional Compilation

  • Preprocessor Functionality:

    • System program operating on source code prior to syntax compilation.

    • Replaces preprocessor commands with expanded text, strips code comments (replacing them with spaces), and collapses duplicate blank lines.

    • Preprocessor directives always begin with a # symbol and do not end with a semicolon ;.

  • Common Preprocessor Directives:

    • #include: Inclusions of external standard or user header files (e.g., #include <stdio.h>, #include <math.h>).

    • #define: Defines symbolic constants or functional macros (e.g., #define PI 3.14159).

    • #undef: Undefines an existing symbolic macro.

    • #if, #ifdef, #ifndef, #else, #elif, #endif: Directives controlling conditional code compilation.

  • Conditional Compilation Code Examples:

    • Example 1: Testing #ifdef:

    #include <stdio.h>
    #define DEBUG
    
    int main() {
    #ifdef DEBUG
        printf("Debug mode is ON");
    #endif
        return 0;
    }
    &nbsp;&nbsp;&nbsp;&nbsp;```
    - *Example 2: Testing `#if` and `#else`*:
    

    c

    include

    define AGE 15

    int main() {

    if AGE >= 18

    printf("Adult");
    

    else

    printf("Minor");
    

    endif

    return 0;
    

    }     ```

    • Example 3: Multi-branch conditional #elif: ```c

    include

    define MARKS 75

    int main() {

    if MARKS >= 90

    printf("Grade A+");
    

    elif MARKS >= 75

    printf("Grade A");
    

    elif MARKS >= 60

    printf("Grade B");
    

    else

    printf("Grade C");
    

    endif

    return 0;
    

    }     ```

Detailed C Compilation, Linking, Loading, and Execution Pipeline

  • The Six-Stage Compilation Pipeline (Acronym: P-C-A-L-L):

    1. Preprocessing:

    • Input: Source code file (program.c).

    • Processing: Handles # directives, expands macros, includes header code, strips comments.

    • Output: Expanded source code file.

    1. Compilation:

    • Input: Expanded source code.

    • Processing: Analyzes syntax/semantics, generates intermediate assembly.

    • Output: Assembly language file (.s file).

    1. Assembly:

    • Input: Assembly language file (.s).

    • Processing: Translates mnemonics into relocatable binary machine code.

    • Output: Object file (.o or .obj file).

    1. Linking:

    • Input: Object files (.o/.obj) + Standard library binary files.

    • Processing: Links multi-file objects, resolves external symbol references (e.g., location of printf()), assigns relative target memory addresses.

    • Output: Binary Executable file (.exe).

    1. Loading:

    • Input: Executable file (.exe).

    • Processing: System OS loader copies executable binary from secondary disk into physical primary RAM memory.

    • Output: Program resident in physical RAM.

    1. Execution:

    • Input: Program instructions in memory.

    • Processing: Central Processing Unit fetches, decodes, and executes binary instructions sequentially.

    • Output: Displayed screen output / computed results.

Compilation Process in C
  • Detailed Role of the Linker:

    • Combines multiple compiled object files into a unified executable file.

    • Binds compiled binary subroutines from standard C library files (e.g., stdio.h binary implementation).

    • Resolves unresolved external function references across separate code modules.

    • Assigns absolute memory addresses to variables and functions.

Role of Linker in C
  • Program Invocation vs Execution:

    • Invocation: The process of initiating or calling a program or function (e.g., issuing hello();).

    • Execution: The operational performance of CPU instructions inside the called routine.

    • Function Call Control Flow:

    1. Program main starts execution.

    2. Function call invocation hello(); pauses main() control flow.

    3. CPU control jumps to hello() function definition.

    4. Statements inside hello() execute.

    5. Function returns control back to the instruction following the call site in main().

Idea of Invocation and ExecutionC Program Lifecycle Flowchart

C Tokens, Identifiers, and Keywords

  • Definition of C Token: The basic, smallest individual lexical unit of a C program.

  • Six Classifications of C Tokens:

    1. Identifiers

    2. Keywords

    3. Constants

    4. Strings

    5. Special Symbols

    6. Operators

C Tokens Classification
  • Identifiers:

    • User-defined symbolic names designated for variables, custom functions, arrays, and structures.

    • Rules for Valid Identifier Names:

    • May contain letters (A-Z, a-z), numbers (0-9), and underscores (_).

    • Must start strictly with a letter or an underscore _ (cannot begin with a digit).

    • Cannot use reserved C keywords.

    • Cannot contain whitespace or special symbols (e.g., $, #, @).

    • Case-sensitive: total, Total, and TOTAL represent three distinct identifiers.

    • Maximum standard identifier length is up to 3030 characters.

    • Examples:

    • Valid: _marks, total1, avg_salary.

    • Invalid: 1total (starts with digit), int (reserved keyword), avg salary (contains space), total# (illegal character).

  • Keywords:

    • Exactly 3232 reserved words in C possessing fixed predefined meanings. All keywords must be written in lowercase.

    • 32 Keywords Categorization:

    • Type-related Keywords (16): int, short, void, enum, float, long, struct, const, char, signed, union, volatile, double, unsigned, typedef, sizeof.

    • Storage-related Keywords (4): auto, static, register, extern.

    • Control flow-related Keywords (12): if, default, goto, for, else, case, continue, while, switch, break, return, do.

Character Sets, Strings, and Escape Sequences

  • String Tokens: Sequences of characters enclosed within double quotes (e.g., `