Mini Projects
In this section, we dive into real-world projects that help you apply your C programming skills. These mini projects are designed to be runnable, self-contained, and focused on core concepts. They’re perfect for reinforcing your understanding and building confidence. 🧮
Calculator
Building a calculator is a classic exercise that teaches you about input handling, arithmetic operations, and basic program flow. Let’s create a simple command-line calculator that supports addition, subtraction, multiplication, and division.
Here’s a minimal implementation that prompts the user for two numbers and an operator:
<code class="language-c">#include <stdio.h>
<p>int main() {</p>
<p> double num1, num2;</p>
<p> char op;</p>
<p> printf("Enter first number: ");</p>
<p> scanf("%lf", &num1);</p>
<p> printf("Enter operator (+, -, *, /): ");</p>
<p> scanf(" %c", &op);</p>
<p> printf("Enter second number: ");</p>
<p> scanf("%lf", &num2);</p>
<p> switch (op) {</p>
<p> case '+':</p>
<p> printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);</p>
<p> break;</p>
<p> case '-':</p>
<p> printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);</p>
<p> break;</p>
<p> case '*':</p>
<p> printf("%.2f <em> %.2f = %.2f\n", num1, num2, num1 </em> num2);</p>
<p> break;</p>
<p> case '/':</p>
<p> if (num2 == 0) {</p>
<p> printf("Error: Division by zero.\n");</p>
<p> } else {</p>
<p> printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);</p>
<p> }</p>
<p> break;</p>
<p> default:</p>
<p> printf("Error: Invalid operator.\n");</p>
<p> }</p>
<p> return 0;</p>
<p>}</code>
Key concepts covered:
- Input handling with
scanf - Type conversion and floating-point arithmetic
switchstatement for operator selection- Basic error handling (division by zero)
This calculator is runnable and immediately useful for practicing input and control flow. Try it out with your own numbers!
Data Structures Implementation
Now, let’s move to a more advanced topic: implementing a stack using a linked list. Stacks are fundamental data structures that follow the LIFO (Last-In-First-Out) principle. By building a stack from scratch, you’ll gain deep insight into memory management and linked lists.
Here’s the implementation:
<code class="language-c">#include <stdio.h>
<p>#include <stdlib.h></p>
<p>typedef struct Node {</p>
<p> int data;</p>
<p> struct Node *next;</p>
<p>} Node;</p>
<p>typedef struct {</p>
<p> Node *top;</p>
<p>} Stack;</p>
<p>void initialize(Stack *s) {</p>
<p> s->top = NULL;</p>
<p>}</p>
<p>void push(Stack *s, int value) {</p>
<p> Node <em>new_node = (Node </em>)malloc(sizeof(Node));</p>
<p> new_node->data = value;</p>
<p> new_node->next = s->top;</p>
<p> s->top = new_node;</p>
<p>}</p>
<p>int pop(Stack *s) {</p>
<p> if (s->top == NULL) {</p>
<p> printf("Stack is empty\n");</p>
<p> return -1;</p>
<p> }</p>
<p> int value = s->top->data;</p>
<p> Node *temp = s->top;</p>
<p> s->top = s->top->next;</p>
<p> free(temp);</p>
<p> return value;</p>
<p>}</p>
<p>int top(Stack *s) {</p>
<p> if (s->top == NULL) {</p>
<p> printf("Stack is empty\n");</p>
<p> return -1;</p>
<p> }</p>
<p> return s->top->data;</p>
<p>}</p>
<p>int isEmpty(Stack *s) {</p>
<p> return (s->top == NULL);</p>
<p>}</p>
<p>int main() {</p>
<p> Stack stack;</p>
<p> initialize(&stack);</p>
<p> push(&stack, 10);</p>
<p> push(&stack, 20);</p>
<p> push(&stack, 30);</p>
<p> printf("Top element: %d\n", top(&stack));</p>
<p> printf("Popped element: %d\n", pop(&stack));</p>
<p> printf("Popped element: %d\n", pop(&stack));</p>
<p> printf("Popped element: %d\n", pop(&stack));</p>
<p> return 0;</p>
<p>}</code>
Why this matters:
- Memory management: We’re using dynamic memory allocation (
malloc) to create nodes. - Linked list operations: Understanding how to add and remove nodes.
- Error handling: Checking for empty stacks to avoid crashes.
This stack implementation is robust and scalable. You can extend it to handle more operations or integrate with other data structures.
Summary
In this section, we’ve built two foundational projects that will help you solidify your C programming skills. Start small, build confidence, and then scale up. These mini projects are your stepping stones to more complex applications. 🚀