WhatsApp

Beginner Guide

C Programming

Learn C language from scratch — setup, syntax, logic, and real programs. Every concept has working code and expected output. No prior programming experience needed.

11Topics
20+Programs
100%Free
Introduction

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?

AreaExample
Operating SystemsLinux kernel, Windows core are written in C
Embedded SystemsMicrocontrollers, Arduino, hardware drivers
System ToolsCompilers, database engines (MySQL core)
Game DevelopmentGame engines and performance-critical code
Learning FoundationFirst language taught in CS programs worldwide
Getting Ready

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:

  1. 1Write code in a .c file — e.g. hello.c
  2. 2Compile — the compiler (GCC) converts your code to machine language
  3. 3Link — connects your code with library functions like printf
  4. 4Run — the executable file runs and shows output
Practical 1

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.

hello.c
#include <stdio.h>    // standard input/output library

int main() {
    printf("Hello, World!\n");   // print to screen
    return 0;                    // 0 means program ran successfully
}
▶ OUTPUT
Hello, World!

Line by line explanation

LineWhat 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.
Practical 2

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.

TypeStoresExampleSize
intWhole numbers5, -10, 1004 bytes
floatDecimal numbers3.14, -2.54 bytes
doublePrecise decimals3.141592658 bytes
charSingle character'A', 'z', '5'1 byte
variables.c
#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;
}
▶ OUTPUT
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.

Practical 3

Input & Output

printf() prints to the screen. scanf() reads input from the keyboard. Together, these two functions handle all basic interaction with the user.

input-output.c
#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;
}
▶ OUTPUT
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.

Practical 4

Operators & Expressions

Operators perform operations on variables and values. C has arithmetic, relational, and logical operators.

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer division)
%Modulus (remainder)10 % 31
==Equal to5 == 51 (true)
!=Not equal5 != 31 (true)
>Greater than7 > 41 (true)
operators.c
#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;
}
▶ OUTPUT
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
Practical 5

Decision Making

Programs need to make decisions based on conditions. C provides if, if-else, and switch for this.

if / else

if-else.c
#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;
}
▶ OUTPUT
Enter marks: 85
Grade: A

switch statement

Use switch when comparing one variable against multiple fixed values — cleaner than many else-if chains.

switch.c — Simple Calculator
#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;
}
▶ OUTPUT
Enter: num1 operator num2
8 * 5
Result = 40
Practical 6

Loops

Loops repeat a block of code multiple times. C has three types — for, while, and do-while.

for loop — Multiplication Table

for-loop.c
#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;
}
▶ OUTPUT
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50

while loop

while-loop.c — Sum of digits
#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;
}
▶ OUTPUT
Enter a number: 456
Sum of digits = 15

do-while loop

Runs the body at least once, then checks the condition. Useful for menus.

do-while.c
#include <stdio.h>

int main() {
    int n = 1;

    do {
        printf("%d ", n);
        n++;
    } while (n <= 5);

    printf("\n");
    return 0;
}
▶ OUTPUT
1 2 3 4 5
Practical 7

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.

functions.c — Add two numbers
#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;
}
▶ OUTPUT
Sum = 30

Recursion

A function that calls itself is called recursive. It must have a base condition to stop, otherwise it runs forever.

recursion.c — Factorial
#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;
}
▶ OUTPUT
Enter n: 5
5! = 120
Practical 8

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.

arrays.c — Store and display marks
#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;
}
▶ OUTPUT
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.

Practice

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.

factorial-loop.c
#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;
}
▶ OUTPUT
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...

fibonacci.c
#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;
}
▶ OUTPUT
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.

prime.c
#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;
}
▶ OUTPUT
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).

palindrome.c
#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;
}
▶ OUTPUT
Enter a number: 121
Reversed: 121
121 is a Palindrome

Find the maximum and minimum values from an array entered by the user.

largest-smallest.c
#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;
}
▶ OUTPUT
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.

bubble-sort.c
#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;
}
▶ OUTPUT
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.

linear-search.c
#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;
}
▶ OUTPUT
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.

pattern.c
#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;
}
▶ OUTPUT (n=5)
*
* *
* * *
* * * *
* * * * *

Read N numbers from the user and calculate their sum and average.

sum-average.c
#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;
}
▶ OUTPUT
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.

even-odd.c
#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;
}
▶ OUTPUT
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.

Keep Learning

Explore more from Devriston

DevOps notes, interview prep, Linux guides, and cloud infrastructure — all free, all practical.