⚡ C Programming Tutorial
Master the foundational language of computer science - from basics to memory management.
01 What is C?
Introduction to procedural programming and the history of C.
02 Setup & First Program
Install GCC and write your very first 'Hello World' in C.
03 Variables & Types
Understand memory and data storage with C types.
04 Operators
Master math and logic operations in C.
05 Control Flow
Make decisions with if-else and switch statements.
06 Functions
Write modular, reusable blocks of code.
07 Arrays & Strings
Master data collections and text manipulation.
08 Pointers & Memory
The 'Zen' of C - understanding memory and addresses.
📑 Table of Contents
What is C?
C is a procedural programming language created in 1972 by Dennis Ritchie. It's known for its efficiency, simplicity, and close-to-hardware nature, making it perfect for systems programming and embedded systems.
Why Learn C?
- Foundation: Understand how computers work at a deeper level
- Performance: Write fast, efficient code
- Embedded Systems: Program microcontrollers and IoT devices
- Unix/Linux: Essential for systems programming
- Career Prospects: High demand in embedded systems and systems programming
Setup & First Program
Installation:
Windows: Download MinGW or use MSVC compiler
Linux:
sudo apt-get install gcc
Mac: Install Xcode Command Line Tools
Your First C Program:
#include
int main() {
printf("Hello, C World!\n");
return 0;
}
Compiling & Running:
gcc program.c -o program
./program
Data Types & Variables
#include
int main() {
// Integer types
int age = 25; // 4 bytes
short year = 2025; // 2 bytes
long population = 8000000000L; // 8 bytes
// Floating point
float height = 5.9f; // 4 bytes
double pi = 3.14159; // 8 bytes
// Character
char initial = 'A'; // 1 byte
// Boolean (using int or stdbool.h)
int is_student = 1; // 1 for true, 0 for false
// String (array of characters)
char name[50] = "John Doe";
// Print values
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Initial: %c\n", initial);
printf("Name: %s\n", name);
return 0;
}
Operators
// Arithmetic operators
int a = 10, b = 3;
printf("a + b = %d\n", a + b); // 13
printf("a - b = %d\n", a - b); // 7
printf("a * b = %d\n", a * b); // 30
printf("a / b = %d\n", a / b); // 3
printf("a %% b = %d\n", a % b); // 1
// Comparison operators
printf("a == b: %d\n", a == b); // 0 (false)
printf("a > b: %d\n", a > b); // 1 (true)
printf("a != b: %d\n", a != b); // 1 (true)
// Logical operators
int x = 1, y = 0;
printf("x && y: %d\n", x && y); // 0 (AND)
printf("x || y: %d\n", x || y); // 1 (OR)
printf("!x: %d\n", !x); // 0 (NOT)
// Assignment operators
int c = 5;
c += 3; // c = c + 3 = 8
c -= 2; // c = c - 2 = 6
c *= 2; // c = c * 2 = 12
Control Flow
// If-Else
int age = 18;
if (age >= 18) {
printf("You are an adult\n");
} else if (age >= 13) {
printf("You are a teenager\n");
} else {
printf("You are a child\n");
}
// Switch
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
default:
printf("Other day\n");
}
// For loop
for (int i = 0; i < 5; i++) {
printf("%d ", i); // 0 1 2 3 4
}
// While loop
int count = 0;
while (count < 3) {
printf("%d ", count);
count++;
}
// Do-While (executes at least once)
int num = 0;
do {
printf("%d ", num);
num++;
} while (num < 3);
Functions
// Function declaration (prototype)
int add(int a, int b);
void greet(char *name);
int main() {
int result = add(5, 3);
printf("Sum: %d\n", result); // 8
greet("Alice");
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
void greet(char *name) {
printf("Hello, %s!\n", name);
}
// Function with multiple return values using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
Arrays & Strings
// Arrays
int numbers[5] = {1, 2, 3, 4, 5};
printf("%d\n", numbers[0]); // 1
// Array iteration
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
// 2D Arrays
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("%d\n", matrix[1][2]); // 6
// Strings
char str1[20] = "Hello";
char str2[] = "World";
// String functions
#include
strlen(str1); // Length
strcpy(str1, str2); // Copy
strcmp(str1, str2); // Compare
strcat(str1, str2); // Concatenate
Pointers
int main() {
int x = 10;
int *ptr = &x; // Pointer to x (& = address of)
printf("x = %d\n", x); // 10
printf("Address of x: %p\n", &x);
printf("ptr = %p\n", ptr); // Same as &x
printf("*ptr = %d\n", *ptr); // 10 (* = dereference)
// Modify value through pointer
*ptr = 20;
printf("x = %d\n", x); // 20
// Array pointers
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr; // Points to first element
printf("%d\n", *p); // 1
printf("%d\n", *(p+1)); // 2
printf("%d\n", p[2]); // 3
// Pointer to pointer
int **pp = &ptr;
printf("%d\n", **pp); // 20
return 0;
}
Structures
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Create and initialize
struct Person p1 = {"John", 25, 5.9};
// Access members
printf("Name: %s\n", p1.name);
printf("Age: %d\n", p1.age);
// Using pointers
struct Person *ptr = &p1;
printf("Height: %.1f\n", ptr->height);
printf("Height: %.1f\n", (*ptr).height); // Alternative
// Array of structures
struct Person people[3] = {
{"Alice", 28, 5.6},
{"Bob", 30, 6.0},
{"Carol", 26, 5.4}
};
for (int i = 0; i < 3; i++) {
printf("%s: %d years old\n", people[i].name, people[i].age);
}
return 0;
}
Projects & Practice
Beginner Projects:
- Calculator
- Number Guessing Game
- Temperature Converter
- Simple Grade Calculator
- Fibonacci Sequence
Intermediate Projects:
- Student Management System
- Library Management System
- File I/O Programs
- Sorting Algorithms
- Hash Table Implementation
Advanced Projects:
- Binary Search Trees
- Graph Algorithms
- Operating System Basics
- Embedded Systems Projects
- Game Development (with graphics library)