C 100 Examples
# C Programming: 100 Classic Practical Examples
Welcome to **YouTip**, your premium developer reference. This comprehensive guide compiles **100 Classic C Programming Examples** designed to help you master C programming from foundational syntax to advanced algorithms, data structures, and system-level operations.
Whether you are preparing for technical interviews, academic exams, or looking to sharpen your low-level programming skills, these structured exercises provide the perfect hands-on practice.
---
## Introduction to C Programming Practice
C is a highly efficient, structured programming language that serves as the foundation for modern operating systems, compilers, and embedded systems. Mastering C requires a deep understanding of memory management, pointers, control flows, and algorithmic logic.
This curated list of 100 exercises is structured to take you from basic input/output operations to complex mathematical computations, array manipulations, pointer operations, and file handling.
---
## Complete Index of the 100 Classic Examples
Below is the categorized index of the 100 classic C programming exercises. Each link represents a core programming concept or algorithmic challenge.
### Basic Logic & Permutations
* [C Exercise 1: Permutations of 3-digit numbers](./c-exercise-example1.html)
* [C Exercise 2: Profit-based bonus calculation](./c-exercise-example2.html)
* [C Exercise 3: Finding a perfect square number](./c-exercise-example3.html)
* [C Exercise 4: Determine the day of the year](./c-exercise-example4.html)
* [C Exercise 5: Sort three integers from smallest to largest](./c-exercise-example5.html)
### Loops & Patterns
* [C Exercise 6: Print the letter 'C' using asterisks](./c-exercise-example6.html)
* [C Exercise 7: Output special characters](./c-exercise-example7.html)
* [C Exercise 8: Output the 9x9 multiplication table](./c-exercise-example8.html)
* [C Exercise 9: Output a chessboard pattern](./c-exercise-example9.html)
* [C Exercise 10: Print a beautiful staircase pattern](./c-exercise-example10.html)
### Classic Algorithms & Math Problems
* [C Exercise 11: The Rabbit Problem (Fibonacci Sequence)](./c-exercise-example11.html)
* [C Exercise 12: Find prime numbers within a range](./c-exercise-example12.html)
* [C Exercise 13: Find all Narcissistic Numbers (Water Lily numbers)](./c-exercise-example13.html)
* [C Exercise 14: Decompose a positive integer into prime factors](./c-exercise-example14.html)
* [C Exercise 15: Conditional operator (`?:`) for grading](./c-exercise-example15.html)
* [C Exercise 16: Find the greatest common divisor (GCD) and least common multiple (LCM)](./c-exercise-example16.html)
* [C Exercise 17: Count characters, spaces, and numbers in a string](./c-exercise-example17.html)
* [C Exercise 18: Calculate the sum of $a + aa + aaa + \dots$](./c-exercise-example18.html)
* [C Exercise 19: Find all Perfect Numbers within 1000](./c-exercise-example19.html)
* [C Exercise 20: Free-fall bounce height calculation](./c-exercise-example20.html)
### Logic Puzzles & Recursion
* [C Exercise 21: The Monkey and the Peaches puzzle](./c-exercise-example21.html)
* [C Exercise 22: Match opponents for two ping-pong teams](./c-exercise-example22.html)
* [C Exercise 23: Print a diamond pattern using asterisks](./c-exercise-example23.html)
* [C Exercise 24: Sum of the first 20 terms of a fractional series](./c-exercise-example24.html)
* [C Exercise 25: Sum of factorials ($1! + 2! + 3! + \dots + 20!$)](./c-exercise-example25.html)
* [C Exercise 26: Compute factorial using recursion](./c-exercise-example26.html)
* [C Exercise 27: Print a string in reverse using recursion](./c-exercise-example27.html)
* [C Exercise 28: Age word problem (Recursive logic)](./c-exercise-example28.html)
* [C Exercise 29: Determine digits of an integer and print in reverse](./c-exercise-example29.html)
* [C Exercise 30: Check if a 5-digit number is a palindrome](./c-exercise-example30.html)
### Arrays, Strings & Pointers
* [C Exercise 31: Determine the day of the week by the first letter](./c-exercise-example31.html)
* [C Exercise 32: Text color customization in console](./c-exercise-example32.html)
* [C Exercise 33: Clear screen implementation](./c-exercise-example33.html)
* [C Exercise 34: Function call syntax practice](./c-exercise-example34.html)
* [C Exercise 35: Reverse an array in place](./c-exercise-example35.html)
* [C Exercise 36: Find prime numbers within 100 using Sieve of Eratosthenes](./c-exercise-example36.html)
* [C Exercise 37: Sort 10 numbers using Selection Sort](./c-exercise-example37.html)
* [C Exercise 38: Sum of the main diagonal elements of a 3x3 matrix](./c-exercise-example38.html)
* [C Exercise 39: Insert a number into a sorted array](./c-exercise-example39.html)
* [C Exercise 40: Reverse the order of elements in an array](./c-exercise-example40.html)
### Variables, Scope & Bitwise Operations
* [C Exercise 41: Static variable (`static`) usage](./c-exercise-example41.html)
* [C Exercise 42: Auto variable (`auto`) usage](./c-exercise-example42.html)
* [C Exercise 43: Static variable scope practice](./c-exercise-example43.html)
* [C Exercise 44: External variable (`extern`) usage](./c-exercise-example44.html)
* [C Exercise 45: Register variable (`register`) usage](./c-exercise-example45.html)
* [C Exercise 46: Macro definition `#define` practice](./c-exercise-example46.html)
* [C Exercise 47: Macro with arguments practice](./c-exercise-example47.html)
* [C Exercise 48: Conditional compilation `#if` and `#ifdef`](./c-exercise-example48.html)
* [C Exercise 49: Conditional compilation `#ifndef` and `#define`](./c-exercise-example49.html)
* [C Exercise 50: Include external files using `#include`](./c-exercise-example50.html)
* [C Exercise 51: Bitwise AND (`&`) operator](./c-exercise-example51.html)
* [C Exercise 52: Bitwise OR (`|`) operator](./c-exercise-example52.html)
* [C Exercise 53: Bitwise XOR (`^`) operator](./c-exercise-example53.html)
* [C Exercise 54: Extract specific bits from an integer](./c-exercise-example54.html)
* [C Exercise 55: Bitwise NOT (`~`) operator](./c-exercise-example55.html)
* [C Exercise 56: Draw circles using graphics library](./c-exercise-example56.html)
* [C Exercise 57: Draw lines using graphics library](./c-exercise-example57.html)
* [C Exercise 58: Draw rectangles using graphics library](./c-exercise-example58.html)
* [C Exercise 59: Draw custom patterns using graphics](./c-exercise-example59.html)
* [C Exercise 60: Draw concentric circles](./c-exercise-example60.html)
### Advanced Data Structures & Pointers
* [C Exercise 61: Print Yang Hui Triangle (Pascal's Triangle)](./c-exercise-example61.html)
* [C Exercise 62: Command-line arguments practice](./c-exercise-example62.html)
* [C Exercise 63: Draw a 3D sphere or custom graphic](./c-exercise-example63.html)
* [C Exercise 64: Draw beautiful curves](./c-exercise-example64.html)
* [C Exercise 65: Draw creative patterns using mathematical functions](./c-exercise-example65.html)
* [C Exercise 66: Input 3 numbers and sort them using pointers](./c-exercise-example66.html)
* [C Exercise 67: Swap the maximum and minimum values in an array](./c-exercise-example67.html)
* [C Exercise 68: Shift array elements to the right cyclically by $M$ positions](./c-exercise-example68.html)
* [C Exercise 69: The Josephus Problem (Counting out game)](./c-exercise-example69.html)
* [C Exercise 70: Find the length of a string using pointers](./c-exercise-example70.html)
* [C Exercise 71: Input and output student records using structures](./c-exercise-example71.html)
* [C Exercise 72: Create a singly linked list](./c-exercise-example72.html)
* [C Exercise 73: Reverse a singly linked list](./c-exercise-example73.html)
* [C Exercise 74: Concatenate two singly linked lists](./c-exercise-example74.html)
* [C Exercise 75: Input and output student records using pointers to structures](./c-exercise-example75.html)
* [C Exercise 76: Evaluate mathematical series using function pointers](./c-exercise-example76.html)
* [C Exercise 77: Access array elements using a pointer to pointer](./c-exercise-example77.html)
* [C Exercise 78: Find the oldest person from a structure array](./c-exercise-example78.html)
* [C Exercise 79: Sort strings alphabetically using pointers](./c-exercise-example79.html)
* [C Exercise 80: Share candy distribution simulation](./c-exercise-example80.html)
### Advanced Algorithms & File I/O
* [C Exercise 81: Solve the equation $809 \times ?? = 800 \times ?? + 9 \times ?? + 1$](./c-exercise-example81.html)
* [C Exercise 82: Convert octal to decimal](./c-exercise-example82.html)
* [C Exercise 83: Count how many odd numbers can be formed](./c-exercise-example83.html)
* [C Exercise 84: Express an even number as the sum of two prime numbers](./c-exercise-example84.html)
* [C Exercise 85: Check if a number is divisible by consecutive 9s](./c-exercise-example85.html)
* [C Exercise 86: Concatenate two strings](./c-exercise-example86.html)
* [C Exercise 87: Structure initialization and member access](./c-exercise-example87.html)
* [C Exercise 88: Read an integer and print that many asterisks](./c-exercise-example88.html)
* [C Exercise 89: Data encryption and decryption algorithm](./c-exercise-example89.html)
* [C Exercise 90: Advanced pointer operations and memory allocation](./c-exercise-example90.html)
* [C Exercise 91: Get the current system time](./c-exercise-example91.html)
* [C Exercise 92: Measure code execution time](./c-exercise-example92.html)
* [C Exercise 93: Measure execution time of a specific function](./c-exercise-example93.html)
* [C Exercise 94: Guess the Number game with time tracking](./c-exercise-example94.html)
* [C Exercise 95: Simple command-line interface (CLI) menu](./c-exercise-example95.html)
* [C Exercise 96: Count occurrences of a substring in a string](./c-exercise-example96.html)
* [C Exercise 97: Read characters from keyboard and write to a file](./c-exercise-example97.html)
* [C Exercise 98: Convert string to uppercase and write to a file](./c-exercise-example98.html)
* [C Exercise 99: Merge two files and sort the contents](./c-exercise-example99.html)
* [C Exercise 100: Save student records to a file](./c-exercise-example100.html)
---
## Additional Practical Examples
Expand your knowledge with these supplementary real-world C programming challenges:
* [**C Matrix Row Exchange**](./c-matrix-exchange.html): Learn how to manipulate multi-dimensional arrays by swapping rows in a matrix using pointers.
* [**The Monkey Eating Peaches Problem**](./c-exercise-example101.html): A classic dynamic programming and recursion problem that calculates initial quantities based on daily decay rates.
---
## Featured Code Example: Permutations of 3-Digit Numbers (Example 1)
To get you started, here is a complete, production-ready implementation of **C Exercise 1**.
### Problem Description
Given the digits `1`, `2`, `3`, and `4`, write a program to find how many mutual, non-repeating 3-digit numbers can be formed, and print all of them.
### C Source Code
```c
#include
int main() {
int i, j, k;
int count = 0; // Counter to keep track of the total permutations
printf("The unique 3-digit numbers are:\n");
// Triple nested loops to generate all permutations of the three digits
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 4; j++) {
for (k = 1; k <= 4; k++) {
// Ensure that the digits are unique and do not repeat
if (i != j && i != k && j != k) {
printf("%d%d%d ", i, j, k);
count++;
// Print a newline every 6 numbers for clean formatting
if (count % 6 == 0) {
printf("\n");
}
}
}
}
}
printf("\nTotal number of unique permutations: %d\n", count);
return 0;
}
```
### Output
```text
The unique 3-digit numbers are:
123 124 132 134 142 143
213 214 231 234 241 243
312 314 321 324 341 342
412 413 421 423 431 432
Total number of unique permutations: 24
```
---
## Key Considerations for C Programming
When writing and compiling C code, keep the following best practices in mind:
1. **Memory Management**: Always free dynamically allocated memory (`malloc`, `calloc`) using `free()` to prevent memory leaks.
2. **Pointer Safety**: Initialize pointers to `NULL` and verify they are not null before dereferencing them.
3. **Array Bounds**: C does not perform automatic bounds checking. Ensure your loops do not write or read past the allocated array size to avoid segmentation faults or buffer overflows.
4. **Compiler Warnings**: Compile your code with warning flags enabled (e.g., `gcc -Wall -Wextra -O2 program.c -o program`) to catch potential bugs early.
YouTip