C Programming Examples
Classic C programs, each with the full source, its exact output, a step-by-step explanation, the mistakes people make (scanf, integer overflow, off-by-one) and a few exercises. Every output was produced by running the program.
Each example opens in the online C Compiler with one click, so you can change it and run it straight away. Everything runs in your browser, with nothing to install.
Also available: Python Examples, JavaScript Examples.
Basics
Input, arithmetic and if/else: the first programs everyone writes.
- C Program to Add Two Numbers
Add two numbers in C with scanf() and printf(), check that scanf() really read them, and use double for decimals and long long for big integers.
Beginner - C Program to Swap Two Numbers
Swap two numbers in C with a temporary variable, then write a swap() function with pointers, and see why a swap function without pointers changes nothing.
Beginner - C Program to Check Whether a Number is Even or Odd
Check whether a number is even or odd in C with the % operator and if/else, see why -3 % 2 is -1, and write the same test with the conditional operator.
Beginner - C Program to Find the Largest of Three Numbers
Find the largest of three numbers in C with if/else if/else and the && operator, then with nested conditional operators, including what happens with ties.
Beginner - C Program to Check Leap Year
Check whether a year is a leap year in C with the full rule (divisible by 4, but centuries only when divisible by 400) in a function that returns a bool.
Beginner
Numbers
Primes, factorials, Fibonacci and other loops over digits and divisors.
- C Program to Check Prime Number
Check whether a number is prime in C by testing divisors up to its square root with i * i <= n in a reusable function, avoiding the bugs that make 9 look prime.
Beginner - C Program to Print Prime Numbers in a Range
Print all prime numbers between two numbers in C with nested loops and a flag variable, then with the Sieve of Eratosthenes using an array of flags.
Intermediate - C Program to Find the Factorial of a Number
Find the factorial of a number in C with a loop and with recursion, use unsigned long long to reach 20!, and detect overflow before it gives wrong answers.
Beginner - C Program to Print the Fibonacci Series
Print the Fibonacci series in C with a for loop, then with recursion, and see why the recursive version gets slow and how unsigned long long holds 93 terms.
Beginner - C Program to Check Armstrong Number
Check whether a number is an Armstrong number in C by counting its digits and summing each digit raised to that power with integer arithmetic only.
Beginner - C Program to Find the Sum of Digits of a Number
Find the sum of the digits of a number in C with a while loop using % 10 and / 10, handle negative numbers, and read the number with scanf().
Beginner - C Program to Reverse a Number
Reverse the digits of a number in C with % and / in a while loop, check whether it is a palindrome number, and avoid overflow when reversing large values.
Beginner - C Program to Find the GCD (HCF) of Two Numbers
Find the GCD (HCF) of two numbers in C with Euclid's algorithm, as a loop and recursively, and compute the LCM from it without overflowing.
Beginner - C Program to Convert Decimal to Binary
Convert a decimal number to binary in C by storing remainders of division by 2 in an array, then with bitwise shifts that print all 8 or 32 bits.
Beginner
Strings
Reversing, counting and comparing text.
- C Program to Check Whether a String is a Palindrome
Check whether a string is a palindrome in C with two indexes moving towards the middle, ignoring case and punctuation with isalnum() and tolower().
Beginner - C Program to Reverse a String
Reverse a string in place in C by swapping characters from both ends, write it as a reusable function, and print a string backwards with a loop.
Beginner - C Program to Count Vowels and Consonants in a String
Count the vowels, consonants, digits and spaces in a string in C by looping until the null terminator and classifying each character with ctype.h.
Beginner - C Program to Find the Length of a String Without strlen()
Find the length of a string in C without strlen() by counting characters up to the null terminator, with an index loop and with pointer subtraction.
Beginner
Arrays and algorithms
Searching, sorting and working with arrays.
- C Program to Find the Largest Element in an Array
Find the largest element of an array in C with a loop, pass arrays to functions with their length, and find the second largest when the maximum repeats.
Beginner - C Program for Bubble Sort
Sort an array with bubble sort in C, stop early when a pass makes no swaps, and compare it with the standard library's qsort() and a comparison function.
Intermediate - C Program for Binary Search
Implement binary search in C on a sorted array with low, high and mid indexes, avoid the classic (low + high) / 2 overflow, and use bsearch() from stdlib.h.
Intermediate - C Program to Multiply Two Matrices
Multiply two matrices in C with 2D arrays and three nested loops, check the dimensions first, and print the result with aligned columns.
Intermediate
Patterns and small projects
Nested loops and complete little programs.
- C Program to Print a Star Pattern (Pyramid)
Print star patterns in C with nested for loops: a right triangle, a centred pyramid and an inverted triangle, and work out the spaces and stars for each row.
Beginner - C Program to Print the Multiplication Table of a Number
Print the multiplication table of a number in C with a for loop and printf field widths for aligned columns, reading the number with scanf().
Beginner - C Program to Make a Simple Calculator Using switch
Make a simple calculator in C with switch...case that reads expressions like "12 * 4" with scanf(), handles division by zero and unknown operators.
Intermediate