Module 1: Introduction to C Programming
Learning Objectives
By the end of this module, you will be able to:
- Understand what programming is.
- Learn the basics of the C programming language.
- Know the history of C.
- Explore the features and applications of C.
- Install the GCC compiler.
- Write and run your first C program.
- Understand how C code is compiled.
- Use comments effectively.
- Build simple beginner projects.
What is Programming?
Programming is the process of writing instructions that tell a computer what to do. These instructions are called programs, and they are written using programming languages.
Think of programming like writing a recipe for a chef. The chef follows each step to prepare a dish. Similarly, a computer follows programming instructions to perform tasks.
Example
Suppose you want a computer to add two numbers.
Algorithm:
- Read first number.
- Read second number.
- Add both numbers.
- Display the result.
Programming converts this algorithm into code.
Why Learn Programming?
Programming allows you to build:
- Websites
- Mobile Applications
- Desktop Software
- Games
- Operating Systems
- Artificial Intelligence Applications
- Embedded Systems
- Robotics Software
What is C?
C is a general-purpose, procedural programming language developed to create efficient and high-performance software.
It is considered the mother of many modern programming languages because languages such as C++, Java, C#, Objective-C, and many others were influenced by C.
C provides direct access to memory and hardware, making it ideal for system programming.
Why is C Still Popular?
- Very fast execution
- Easy to learn syntax
- Small runtime
- Portable across platforms
- Used in operating systems
- Excellent for learning programming fundamentals
History of C
The C programming language has a rich history.
Timeline
1967
Martin Richards created the BCPL language.
↓
1970
Ken Thompson developed the B language.
↓
1972
Dennis Ritchie developed C at Bell Laboratories.
↓
1978
The famous book The C Programming Language by Brian Kernighan and Dennis Ritchie (often called K&R C) was published.
↓
1989
ANSI standardized C as ANSI C (C89).
↓
Later Standards
- C90
- C99
- C11
- C17
- C23
Today, modern compilers support these standards while remaining compatible with older C code.
Features of C
C has many features that make it one of the most influential programming languages.
1. Simple
C has a small set of keywords and a straightforward syntax.
1int age = 20;
2. Fast
Programs written in C execute very quickly because they are compiled directly into machine code.
3. Portable
A C program written on Windows can usually be compiled on Linux or macOS with little or no modification.
4. Structured Programming
Large programs can be divided into smaller functions.
1void displayMessage() 2{ 3 printf("Welcome!"); 4}
5. Rich Library
C provides many standard library functions.
Example:
1printf(); 2scanf(); 3strlen(); 4sqrt();
6. Efficient Memory Management
Programmers can directly allocate and free memory.
1malloc() 2free()
7. Middle-Level Language
C combines the features of both:
- High-level programming
- Low-level programming
This makes it suitable for system software.
8. Extensible
Programmers can create their own functions and libraries.
Applications of C
C is used in many industries.
Operating Systems
Examples:
- Linux
- UNIX
- Windows Kernel (parts)
Embedded Systems
Examples:
- Washing Machines
- Smart TVs
- Routers
- IoT Devices
Database Systems
Examples:
- MySQL
- PostgreSQL (core components)
Compilers
Many compilers are written in C.
Game Engines
Performance-critical game engine components often use C.
Device Drivers
Hardware drivers commonly use C for direct hardware interaction.
Networking Software
Examples:
- Network tools
- Protocol implementations
- Packet analyzers
Scientific Computing
High-performance scientific software often uses C.
Installing GCC Compiler
GCC (GNU Compiler Collection) is one of the most widely used compilers for C.
Windows
Option 1: MinGW-w64
- Download MinGW-w64.
- Install the compiler.
- Add the
bindirectory to the system PATH. - Open Command Prompt.
- Verify the installation:
1gcc --version
Option 2: MSYS2
Install MSYS2 and use its package manager to install GCC.
Linux (Ubuntu/Debian)
Update package information and install GCC:
1sudo apt update 2sudo apt install build-essential
Check the version:
1gcc --version
Fedora
1sudo dnf install gcc
Arch Linux
1sudo pacman -S gcc
macOS
Install Apple's Command Line Tools:
1xcode-select --install
Or install GCC using Homebrew:
1brew install gcc
Writing Your First C Program
Create a file named:
hello.c
Write the following code:
1#include <stdio.h> 2 3int main() 4{ 5 printf("Hello, World!\n"); 6 return 0; 7}
Explanation
1#include <stdio.h>
Includes the Standard Input/Output library, allowing the use of functions like printf().
1int main()
The main() function is the entry point of every C program.
1printf("Hello, World!\n");
Prints text to the screen.
1return 0;
Returns a value to the operating system indicating successful execution.
Compiling and Running the Program
Open a terminal in the folder containing hello.c.
Compile the program:
1gcc hello.c -o hello
Explanation
gcc→ Compilerhello.c→ Source file-o hello→ Output executable namedhello
Run the executable:
Linux/macOS
1./hello
Windows
1hello.exe
Output:
1Hello, World!
Compilation Process
When you compile a C program, it goes through several stages.
Source Code (.c)
│
▼
Preprocessor
│
▼
Compiler
│
▼
Assembler
│
▼
Object File (.o/.obj)
│
▼
Linker
│
▼
Executable Program
1. Preprocessor
Processes directives such as:
1#include 2#define
2. Compiler
Converts C code into assembly language.
3. Assembler
Converts assembly language into machine code (object files).
4. Linker
Combines object files and required libraries into the final executable.
Comments in C
Comments improve readability and explain the purpose of code.
The compiler ignores comments.
Single-Line Comment
1// This is a single-line comment
Example:
1// Display welcome message 2printf("Welcome");
Multi-Line Comment
1/* 2This is a 3multi-line comment. 4*/
Example:
1/************************************************ 2Program : Calculator 3Author : John 4Date : 2026 5************************************************/
Best Practices for Comments
- Explain why code exists, not what every line does.
- Keep comments up to date.
- Avoid unnecessary comments for obvious code.
- Use descriptive block comments for files and complex functions.
Practice Project 1: Hello World
1#include <stdio.h> 2 3int main() 4{ 5 printf("Welcome to C Programming!\n"); 6 return 0; 7}
Practice Project 2: Simple Calculator
1#include <stdio.h> 2 3int main() 4{ 5 int a, b; 6 7 printf("Enter two numbers: "); 8 scanf("%d %d", &a, &b); 9 10 printf("Addition = %d\n", a + b); 11 printf("Subtraction = %d\n", a - b); 12 printf("Multiplication = %d\n", a * b); 13 14 if (b != 0) 15 printf("Division = %.2f\n", (float)a / b); 16 else 17 printf("Division by zero is not allowed.\n"); 18 19 return 0; 20}
Practice Project 3: Student Information
1#include <stdio.h> 2 3int main() 4{ 5 char name[50]; 6 int age; 7 float marks; 8 9 printf("Enter Name: "); 10 scanf("%49s", name); 11 12 printf("Enter Age: "); 13 scanf("%d", &age); 14 15 printf("Enter Marks: "); 16 scanf("%f", &marks); 17 18 printf("\n----- Student Information -----\n"); 19 printf("Name : %s\n", name); 20 printf("Age : %d\n", age); 21 printf("Marks : %.2f\n", marks); 22 23 return 0; 24}
Module Summary
In this module, you learned:
- What programming is and why it matters.
- What the C programming language is and its historical background.
- Key features that make C powerful and widely used.
- Real-world applications of C in operating systems, embedded systems, databases, and networking.
- How to install the GCC compiler on Windows, Linux, and macOS.
- How to write, compile, and run your first C program.
- The stages of the C compilation process.
- How to use single-line and multi-line comments effectively.
- How to build simple beginner projects such as a Hello World application, a basic calculator, and a student information program.
This foundation prepares you for the next module, where you'll explore variables, data types, input/output, constants, and operators in C.