ccompiler.inOnline C Compiler & Docs
Code Examples & Lab Solutions

C Example Programs

Master C programming by writing and running practical code. Browse our categorized collection of tested C programs covering essential loops, arrays, pointer algorithms, matrix computations, sorting routines, and foundational data structures.

Example Program

Area of Shapes Program in C — Geometric Calculations

Calculate the area of geometric shapes (circle, triangle, rectangle, sphere) in C using functions, mathematical constants, and standard math.h.

View Code & Output
Example Program

ASCII Value Program in C — Character Code Inspector

Print ASCII values of characters, numbers, and symbols in C using format specifiers. Explore ASCII conversion tables and character arithmetic.

View Code & Output
Example Program

String to Integer (atoi) in C — Custom Implementation

Implement your own atoi (ASCII to Integer) function in C from scratch. Handle leading whitespace, positive/negative signs, and integer overflow.

View Code & Output
Example Program

Binary Tree Traversal in C — Preorder, Inorder, Postorder

Implement Binary Tree traversals in C: Inorder, Preorder, and Postorder using recursive depth-first algorithms with full C source code.

View Code & Output
Example Program

Circular Queue in C — Array Implementation with Modulo

Implement a Circular Queue in C using modulo arithmetic to prevent memory wastage. Complete code for circular enqueue, dequeue, and display.

View Code & Output
Example Program

Command Line Arguments in C — argc and argv Guide

Learn how to parse command line arguments in C using argc and argv. Complete working code for flag parsing, option switches, and type casting.

View Code & Output
Example Program

File Handling Read & Write in C — fopen, fread, fwrite

Master text and binary file I/O operations in C using fopen, fprintf, fscanf, fread, and fwrite with error handling and buffer flushing.

View Code & Output
Example Program

Function Pointers in C — Callback & Dispatcher Guide

Master function pointers in C with syntax rules, callback function handlers, comparator sorting, and jump tables with full runnable code.

View Code & Output
Example Program

Integer to String (itoa) in C — Custom Implementation

Convert integers to character strings in standard C without non-standard itoa. Full working code for arbitrary bases, signs, and buffer safety.

View Code & Output
Example Program

Number Pattern Programs in C — Triangle, Floyd & Matrix

Master number patterns in C including Floyd's triangle, continuous number pyramids, and binary alternating grids with runnable C programs.

View Code & Output
Example Program

Perfect Number in C — Proper Divisor Sum Algorithm

Check if an integer is a perfect number in C by summing its proper divisors. Optimized O(sqrt(n)) algorithm with working source code.

View Code & Output
Example Program

Sieve of Eratosthenes in C — Prime Generator Algorithm

Generate prime numbers up to N efficiently in C using the Sieve of Eratosthenes. Full working code, boolean array sieve, and O(n log log n) trace.

View Code & Output
Example Program

Stack Using Linked List in C — Dynamic Push & Pop

Implement a dynamic stack data structure in C using singly linked list nodes. Complete code for push, pop, peek, and heap memory management.

View Code & Output
Example Program

Star Pattern Programs in C — Pyramid, Diamond & Triangle

Learn how to print star patterns in C: right-angled triangle, inverted pyramid, and diamond shapes using nested loops with full code.

View Code & Output
Example Program

Static vs Global Variables in C — Scope & Lifetime Guide

Understand static vs global variables in C: file scope vs internal linkage, stack vs data segment memory, lifetimes, and storage classes.

View Code & Output
Example Program

Strong Number in C — Factorial Sum of Digits Program

Learn how to check if a number is a Strong (Krishnamurthy) number in C by calculating the sum of factorials of digits with complete code.

View Code & Output
Example Program

Structures and Unions in C — Memory Allocation & Syntax

Master the difference between structs and unions in C with memory layout diagrams, byte alignment, sizeof demonstrations, and working examples.

View Code & Output
Example Program

Temperature Conversion in C — Celsius, Fahrenheit & Kelvin

Build a temperature conversion program in C to convert between Celsius, Fahrenheit, and Kelvin using precise floating-point formulas.

View Code & Output
Example Program

Armstrong Number in C — Check & Print Narcissistic Numbers

Learn how to check if a number is an Armstrong (narcissistic) number in C for any number of digits with full working code and complexity analysis.

View Code & Output
Example Program

Binary Search in C — Iterative & Recursive Approaches

Implement Binary Search in C on sorted arrays. Iterative and recursive algorithms explained with mid-point calculations and overflow prevention.

View Code & Output
Example Program

Count Vowels and Consonants in C — String Parser

Write a C program to count vowels, consonants, digits, and whitespace in a string using standard ctype.h classification functions.

View Code & Output
Example Program

Dynamic Memory Allocation in C — malloc, calloc & free

Master dynamic heap memory allocation in C with malloc, calloc, realloc, and free. Step-by-step memory safety and leak prevention guide.

View Code & Output
Example Program

Factorial Program in C — Iterative & Recursive Logic

Compute the factorial of a number in C using iterative loops and recursive functions. Full code, step trace, and overflow handling explained.

View Code & Output
Example Program

GCD and LCM in C — Euclidean Algorithm & Formulas

Calculate Greatest Common Divisor (GCD) and Least Common Multiple (LCM) in C using Euclidean algorithm and modular arithmetic with examples.

View Code & Output
Example Program

Insertion Sort in C — Algorithm, Code & Trace

Learn Insertion Sort in C with step-by-step element insertion trace, full runnable source code, and time complexity comparison explained.

View Code & Output
Example Program

Leap Year Program in C — Gregorian Calendar Rules

Check whether a given year is a leap year in C using Gregorian calendar conditional logic with multiple approaches and test examples.

View Code & Output
Example Program

Linear Search in C — Algorithm, Code & Time Complexity

Implement Linear Search in C for unsorted arrays. Step-by-step element search trace, best/worst case comparisons, and sample code included.

View Code & Output
Example Program

Matrix Addition in C — 2D Array Arithmetic Guide

Add two matrices in C using 2D arrays and nested loops. Includes dimension checks, code implementation, and element-wise addition trace.

View Code & Output
Example Program

Merge Sort in C — Divide & Conquer Algorithm Guide

Learn Merge Sort in C with recursive splitting, array merging logic, full working source code, and guaranteed O(n log n) complexity.

View Code & Output
Example Program

Pascal's Triangle in C — Combinations & Array Logic

Print Pascal's Triangle in C using binomial combinations formula and 2D dynamic arrays. Step-by-step coefficient calculations explained.

View Code & Output
Example Program

Queue Implementation in C Using Array — Enqueue & Dequeue

Implement a FIFO Queue data structure in C using fixed arrays. Complete code for enqueue, dequeue, peek, and capacity overflow management.

View Code & Output
Example Program

Quicksort in C — Divide & Conquer Algorithm Guide

Implement Quicksort in C using Lomuto partitioning. Includes recursion stack trace, pivot selection strategies, and complexity analysis.

View Code & Output
Example Program

Remove Duplicates from String in C — Fast Frequency Hash

Remove duplicate characters from a string in C using a frequency hash map and two pointers with O(n) linear time complexity.

View Code & Output
Example Program

Reverse a Number in C — Integer & Pointer Methods

Reverse an integer in C with full source code, step-by-step logic, negative number handling, and integer overflow protection explained.

View Code & Output
Example Program

Selection Sort in C — Algorithm, Code & Trace

Implement Selection Sort in C with step-by-step logic, code, array swapping with pointers, and full time and space complexity breakdown.

View Code & Output
Example Program

Simple Calculator in C — Switch Case & Arithmetic

Build a command-line calculator in C using switch-case statements, arithmetic operators, division-by-zero checks, and modular functions.

View Code & Output
Example Program

Singly Linked List in C — Insert, Delete & Traverse

Implement a Singly Linked List in C with dynamic memory allocation (malloc/free). Full code for node insertion, deletion, and traversal.

View Code & Output
Example Program

Stack Implementation in C Using Array — Push & Pop

Implement a Stack data structure in C using static arrays. Complete runnable code for push, pop, peek, isFull, and isEmpty operations.

View Code & Output
Example Program

Reverse a String in C — Pointer & In-Place Methods

Reverse a character string in C in-place using two-pointer swapping and without using strrev. Full working code and memory safety explained.

View Code & Output
Example Program

Sum of Digits in C — Iterative & Recursive Programs

Calculate the sum of digits of an integer in C using loops and recursion. Step-by-step trace, edge cases, and code examples explained.

View Code & Output
Example Program

Tower of Hanoi in C — Recursion Algorithm & Trace

Solve the classic Tower of Hanoi puzzle in C with recursive algorithms, disk move tracing, step counts, and 2^n - 1 complexity explained.

View Code & Output
Example Program

Transpose of a Matrix in C — 2D Array Transformation

Compute the transpose of a matrix in C by swapping row and column indices. Complete runnable C code and square matrix in-place swap logic.

View Code & Output
Example Program

Bubble Sort in C — Algorithm & Optimized Code

Implement Bubble Sort in C with optimized early exit flags. Includes step-by-step trace, memory comparisons, and time complexity analysis.

View Code & Output
Example Program

Fibonacci Series in C — Iterative & Recursive Code

Generate the Fibonacci series in C using iterative loops and recursive functions. Complete code with complexity analysis and output.

View Code & Output
Example Program

Matrix Multiplication in C — 2D Array Algorithm

Multiply two matrices in C using 2D arrays and nested loops. Complete validated source code, dimension validation rules, and complexity breakdown.

View Code & Output
Example Program

Palindrome Program in C — String & Number Check

Check if a number or string is a palindrome in C with complete tested code, step-by-step arithmetic trace, and string pointer examples.

View Code & Output
Example Program

Prime Number Program in C — Optimized Algorithm

Check if a number is prime and print prime numbers in a range in C using optimized sqrt(n) algorithms with complete source code.

View Code & Output
Example Program

Swap Two Numbers in C — Pointers & Bitwise Methods

Learn how to swap two numbers in C using temporary variables, arithmetic operations, bitwise XOR, and pass-by-reference pointers.

View Code & Output