Unit 2: Art of Programming and C Overview

Table of Contents

1. Art of Programming through Algorithms and Flowcharts

Before writing code, a programmer must plan the logic. This "art of programming" is done using algorithms and flowcharts.

An Algorithm is a finite, step-by-step, unambiguous set of instructions to solve a specific problem.

Example: Algorithm to add two numbers

  1. Start
  2. Declare three variables: num1, num2, sum.
  3. Read values for num1 and num2.
  4. Calculate sum = num1 + num2.
  5. Display sum.
  6. Stop

Qualities of a Good Algorithm

A good algorithm must have the following properties:

Flowchart Symbols

A Flowchart is a graphical or pictorial representation of an algorithm. It uses standard symbols to show the sequence of operations.

Symbol Name Function
Oval / Terminal Start / Stop Indicates the beginning or end of the flowchart.
Parallelogram Input / Output Used for reading data (Input) or displaying results (Output).
Rectangle Process Represents an operation or calculation (e.g., sum = a + b).
Diamond Decision Used for asking a question (e.g., is a > b?). Has one entry and two exits (Yes/No).
Arrows Flow Lines Show the direction and sequence of operations.

Rules for designing Flowcharts

2. Overview of C Programming

History and Importance of C

Basic Structure of C program

Every C program follows a standard structure.

#include   // Preprocessor Directive

// Global declarations (if any)

int main() {        // The main function where execution begins
    
    // Variable declarations
    int num;
    
    // Program statements
    printf("Hello, World!");
    
    return 0;   // Return statement
}

Executing a C program

The process from writing code to getting an output involves several steps:

  1. Create (Write) Source Code: Save your program as a .c file (e.g., hello.c).
  2. Compile: Use a compiler (like gcc) to translate the source code into machine-readable object code (hello.o). The compiler also checks for syntax errors.
  3. Link: A Linker combines your object code with any necessary library code (like the code for printf) to create a final executable file (e.g., hello.exe or a.out).
  4. Execute (Run): You run the executable file, and the computer performs the instructions.