What is C Language?
C is one of the oldest and most widely-used programming languages in the world. It was developed in the early 1970s at Bell Labs. Almost every operating system — including Linux and Windows — is written using C or C-based languages.
If you're learning programming for the first time, C is an excellent starting point. It teaches you how computers actually work — memory, variables, logic, and how programs execute line by line.
Why learn C first? C teaches the fundamentals that every other language builds on — variables, loops, functions, memory. Once you know C, learning Python, Java, or C++ becomes much easier.
Where is C used?
| Area | Example |
|---|---|
| Operating Systems | Linux kernel, Windows core are written in C |
| Embedded Systems | Microcontrollers, Arduino, hardware drivers |
| System Tools | Compilers, database engines (MySQL core) |
| Game Development | Game engines and performance-critical code |
| Learning Foundation | First language taught in CS programs worldwide |
Setup & Install
You have two options — write code online (nothing to install) or install a compiler on your computer. Both work fine for learning.
Online Compiler
Run C code in your browser. No installation needed. Best for beginners.
OnlineGDB →Programiz →
Dev-C++ (Offline)
Simple IDE for Windows. Classic choice for beginners. Easy to install.
Download Dev-C++ →VS Code + GCC
Professional setup. Lightweight editor with full GCC compiler support.
Download VS Code →MinGW (GCC) →
Recommendation: Start with OnlineGDB — no setup required, runs instantly. Once you're comfortable with the basics, install Dev-C++ or VS Code for offline practice.
How a C program runs
Every C program goes through these steps before you see output:
- 1Write code in a
.cfile — e.g.hello.c - 2Compile — the compiler (GCC) converts your code to machine language
- 3Link — connects your code with library functions like
printf - 4Run — the executable file runs and shows output
Hello World Start Here
Every programmer writes this first. It teaches you the basic structure of a C program — what every file must contain before anything else can work.
#include <stdio.h> // standard input/output library int main() { printf("Hello, World!\n"); // print to screen return 0; // 0 means program ran successfully }
Hello, World!
Line by line explanation
| Line | What it does |
|---|---|
| #include <stdio.h> | Imports the standard library so you can use printf and scanf |
| int main() | Every C program starts execution from main(). The int means it returns a number. |
| printf("...") | Prints text to the screen. \n moves to the next line. |
| return 0; | Tells the OS the program finished without errors. |
Variables & Data Types
A variable is a named box that stores data. Before using a variable in C, you must declare its type — this tells the compiler how much memory to reserve and what kind of data it will hold.
| Type | Stores | Example | Size |
|---|---|---|---|
| int | Whole numbers | 5, -10, 100 | 4 bytes |
| float | Decimal numbers | 3.14, -2.5 | 4 bytes |
| double | Precise decimals | 3.14159265 | 8 bytes |
| char | Single character | 'A', 'z', '5' | 1 byte |
#include <stdio.h> int main() { int age = 18; float marks = 87.5; char grade = 'A'; printf("Age: %d\n", age); printf("Marks: %.1f\n", marks); printf("Grade: %c\n", grade); return 0; }
Age: 18 Marks: 87.5 Grade: A
Format specifiers: %d = integer, %f = float, %c = character, %s = string. These go inside printf/scanf to match the variable type.
Input & Output
printf() prints to the screen. scanf() reads input from the keyboard. Together, these two functions handle all basic interaction with the user.
#include <stdio.h> int main() { int num1, num2, sum; printf("Enter first number: "); scanf("%d", &num1); // & means "address of" num1 printf("Enter second number: "); scanf("%d", &num2); sum = num1 + num2; printf("Sum = %d\n", sum); return 0; }
Enter first number: 10 Enter second number: 25 Sum = 35
Common mistake: Forgetting & before the variable name in scanf. Always write scanf("%d", &variable) — the ampersand is required.
Operators & Expressions
Operators perform operations on variables and values. C has arithmetic, relational, and logical operators.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3 (integer division) |
| % | Modulus (remainder) | 10 % 3 | 1 |
| == | Equal to | 5 == 5 | 1 (true) |
| != | Not equal | 5 != 3 | 1 (true) |
| > | Greater than | 7 > 4 | 1 (true) |
#include <stdio.h> int main() { int a = 17, b = 5; printf("a + b = %d\n", a + b); printf("a - b = %d\n", a - b); printf("a * b = %d\n", a * b); printf("a / b = %d\n", a / b); // integer division = 3 printf("a %% b = %d\n", a % b); // remainder = 2 return 0; }
a + b = 22 a - b = 12 a * b = 85 a / b = 3 a % b = 2
Decision Making
Programs need to make decisions based on conditions. C provides if, if-else, and switch for this.
if / else
#include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); if (marks >= 90) { printf("Grade: A+\n"); } else if (marks >= 80) { printf("Grade: A\n"); } else if (marks >= 70) { printf("Grade: B\n"); } else if (marks >= 60) { printf("Grade: C\n"); } else { printf("Grade: Fail\n"); } return 0; }
Enter marks: 85 Grade: A
switch statement
Use switch when comparing one variable against multiple fixed values — cleaner than many else-if chains.
#include <stdio.h> int main() { int a, b; char op; printf("Enter: num1 operator num2\n"); scanf("%d %c %d", &a, &op, &b); switch (op) { case '+': printf("Result = %d\n", a + b); break; case '-': printf("Result = %d\n", a - b); break; case '*': printf("Result = %d\n", a * b); break; case '/': if (b != 0) printf("Result = %d\n", a / b); else printf("Error: Division by zero\n"); break; default: printf("Unknown operator\n"); } return 0; }
Enter: num1 operator num2 8 * 5 Result = 40
Loops
Loops repeat a block of code multiple times. C has three types — for, while, and do-while.
for loop — Multiplication Table
#include <stdio.h> int main() { int n; printf("Enter a number: "); scanf("%d", &n); for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); } return 0; }
Enter a number: 5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50
while loop
#include <stdio.h> int main() { int num, sum = 0, digit; printf("Enter a number: "); scanf("%d", &num); while (num != 0) { digit = num % 10; // get last digit sum += digit; num /= 10; // remove last digit } printf("Sum of digits = %d\n", sum); return 0; }
Enter a number: 456 Sum of digits = 15
do-while loop
Runs the body at least once, then checks the condition. Useful for menus.
#include <stdio.h> int main() { int n = 1; do { printf("%d ", n); n++; } while (n <= 5); printf("\n"); return 0; }
1 2 3 4 5
Functions
A function is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, you write it once as a function and call it whenever needed.
#include <stdio.h> // Function declaration int add(int a, int b); int main() { int x = 10, y = 20; int result = add(x, y); // function call printf("Sum = %d\n", result); return 0; } // Function definition int add(int a, int b) { return a + b; }
Sum = 30
Recursion
A function that calls itself is called recursive. It must have a base condition to stop, otherwise it runs forever.
#include <stdio.h> int factorial(int n) { if (n == 0 || n == 1) return 1; // base condition return n * factorial(n - 1); // recursive call } int main() { int n; printf("Enter n: "); scanf("%d", &n); printf("%d! = %d\n", n, factorial(n)); return 0; }
Enter n: 5 5! = 120
Arrays
An array stores multiple values of the same type in a single variable. Instead of creating 10 separate variables for 10 numbers, you create one array of size 10.
#include <stdio.h> int main() { int marks[5]; int sum = 0; printf("Enter 5 marks:\n"); for (int i = 0; i < 5; i++) { scanf("%d", &marks[i]); sum += marks[i]; } printf("Total = %d\n", sum); printf("Average = %.1f\n", sum / 5.0); return 0; }
Enter 5 marks: 70 80 90 85 75 Total = 400 Average = 80.0
Array indexing starts at 0. An array int marks[5] has elements at positions 0, 1, 2, 3, 4. Accessing marks[5] goes out of bounds and causes undefined behavior.
Important Programs Exam Ready
These are the most commonly asked C programs in exams and lab practicals. Each one builds on the concepts covered above. Click any program to expand it.
Calculate n! using a for loop. Factorial of 5 = 5×4×3×2×1 = 120.
#include <stdio.h> int main() { int n, fact = 1; printf("Enter n: "); scanf("%d", &n); for (int i = 1; i <= n; i++) fact *= i; printf("%d! = %d\n", n, fact); return 0; }
Enter n: 5 5! = 120
Print the Fibonacci series up to n terms. Each number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8...
#include <stdio.h> int main() { int n, a = 0, b = 1, c; printf("How many terms? "); scanf("%d", &n); printf("Fibonacci: "); for (int i = 1; i <= n; i++) { printf("%d ", a); c = a + b; a = b; b = c; } printf("\n"); return 0; }
How many terms? 7 Fibonacci: 0 1 1 2 3 5 8
A prime number is divisible only by 1 and itself. Check if a given number is prime.
#include <stdio.h> int main() { int n, isPrime = 1; printf("Enter a number: "); scanf("%d", &n); if (n < 2) isPrime = 0; for (int i = 2; i < n; i++) { if (n % i == 0) { isPrime = 0; break; } } if (isPrime) printf("%d is a Prime number\n", n); else printf("%d is NOT a Prime number\n", n); return 0; }
Enter a number: 13 13 is a Prime number
Reverse the digits of a number. If reversed == original, it's a palindrome (e.g. 121, 1331).
#include <stdio.h> int main() { int num, original, rev = 0; printf("Enter a number: "); scanf("%d", &num); original = num; while (num != 0) { rev = rev * 10 + (num % 10); num /= 10; } printf("Reversed: %d\n", rev); if (original == rev) printf("%d is a Palindrome\n", original); else printf("%d is NOT a Palindrome\n", original); return 0; }
Enter a number: 121 Reversed: 121 121 is a Palindrome
Find the maximum and minimum values from an array entered by the user.
#include <stdio.h> int main() { int n, arr[50]; printf("How many numbers? "); scanf("%d", &n); printf("Enter %d numbers:\n", n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); int max = arr[0], min = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; } printf("Largest = %d\n", max); printf("Smallest = %d\n", min); return 0; }
How many numbers? 5 Enter 5 numbers: 34 12 78 56 90 Largest = 90 Smallest = 12
Sort an array in ascending order by repeatedly swapping adjacent elements that are in the wrong order.
#include <stdio.h> int main() { int n, arr[50], temp; printf("How many numbers? "); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); // Bubble sort for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } printf("Sorted: "); for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); return 0; }
How many numbers? 5 64 34 25 12 22 Sorted: 12 22 25 34 64
Search for a value in an array by checking each element one by one from start to end.
#include <stdio.h> int main() { int arr[] = {10, 25, 38, 47, 55}; int n = 5, target, found = 0; printf("Enter number to search: "); scanf("%d", &target); for (int i = 0; i < n; i++) { if (arr[i] == target) { printf("Found at index %d\n", i); found = 1; break; } } if (!found) printf("Number not found\n"); return 0; }
Enter number to search: 38 Found at index 2
Print a right-triangle star pattern using nested loops. Classic exam question for testing loop understanding.
#include <stdio.h> int main() { int n; printf("Enter rows: "); scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; }
* * * * * * * * * * * * * * *
Read N numbers from the user and calculate their sum and average.
#include <stdio.h> int main() { int n, num, sum = 0; printf("How many numbers? "); scanf("%d", &n); for (int i = 1; i <= n; i++) { printf("Enter number %d: ", i); scanf("%d", &num); sum += num; } printf("Sum = %d\n", sum); printf("Average = %.2f\n", (float)sum / n); return 0; }
How many numbers? 3 Enter number 1: 10 Enter number 2: 20 Enter number 3: 30 Sum = 60 Average = 20.00
Read an array and count how many numbers are even and how many are odd using the modulus operator.
#include <stdio.h> int main() { int n, arr[50], even = 0, odd = 0; printf("How many numbers? "); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); if (arr[i] % 2 == 0) even++; else odd++; } printf("Even numbers: %d\n", even); printf("Odd numbers: %d\n", odd); return 0; }
How many numbers? 6 1 2 3 4 5 6 Even numbers: 3 Odd numbers: 3
More programs coming. This page is regularly updated. Bookmark it and check back for more practicals including string programs, 2D arrays, and number system conversions.