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
- Start
- Declare three variables:
num1,num2,sum. - Read values for
num1andnum2. - Calculate
sum = num1 + num2. - Display
sum. - Stop
Qualities of a Good Algorithm
A good algorithm must have the following properties:
- Input: It must take zero or more well-defined inputs.
- Output: It must produce one or more well-defined outputs.
- Finiteness: It must terminate after a finite number of steps.
- Definiteness/Unambiguous: Each step must be clear and precisely defined.
- Effectiveness: Each step must be simple enough to be carried out (e.g., "add two numbers" is effective; "find the best move" is not).
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
- A flowchart should have only one Start and one Stop symbol.
- The flow of logic should generally be from top to bottom or left to right.
- A decision (diamond) symbol must have two exit points (Yes, No).
- All symbols must be connected by flow lines (arrows).
2. Overview of C Programming
History and Importance of C
- History: C was developed in the early 1970s by Dennis Ritchie at Bell Labs. It was created to write the UNIX operating system.
- Importance:
- It is a "middle-level" language: it has high-level features (portability, easy-to-read) and low-level features (pointer manipulation, direct memory access).
- It is extremely fast and efficient.
- It is the foundation for many other languages (C++, C#, Java, Python) and modern operating systems (Windows, Linux, macOS).
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 }
#include: A preprocessor directive that tells the compiler to include the "Standard Input/Output" header file (which containsprintf,scanf, etc.).int main(): The main function. Every C program must have one. Execution *always* starts here.{ ... }: The curly braces define the "body" or block of the function.return 0;: Tells the operating system that the program executed successfully.
Executing a C program
The process from writing code to getting an output involves several steps:
- Create (Write) Source Code: Save your program as a
.cfile (e.g.,hello.c). - 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. - 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.exeora.out). - Execute (Run): You run the executable file, and the computer performs the instructions.