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
num1, num2, sum.num1 and num2.sum = num1 + num2.sum.A good algorithm must have the following properties:
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. |
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 contains printf, 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.The process from writing code to getting an output involves several steps:
.c file (e.g., hello.c).gcc) to translate the source code into machine-readable object code (hello.o). The compiler also checks for syntax errors.printf) to create a final executable file (e.g., hello.exe or a.out).