C guide
  • Hello reader!
  • C programming
    • C
    • Roadmap
    • Basics
      • Variables in C
      • I/O in C
      • Data types
      • Operators
      • Control Flow
      • Arrays
        • Strings (char arrays)
        • Multidimensional Arrays
    • Functions!
      • Recursion
  • Pointers
    • Pointer-Array Relationship
  • Structures and Unions
  • Dynamic memory allocation
  • File I/O (Input/Output)
  • Advanced topics
  • Debugging & Optimization
  • Practices
  • Cheatsheet
Powered by GitBook
On this page

Was this helpful?

Practices

1. Mini Projects

1.1. Simple Calculator

A basic calculator that performs addition, subtraction, multiplication, and division.

Features:

  • Accepts two numbers and an operator from the user.

  • Performs the chosen operation.

  • Displays the result.

Example Code:

#include <stdio.h>

int main() {
    char operator;
    double num1, num2, result;

    // User input
    printf("Enter an operator (+, -, *, /): ");
    scanf(" %c", &operator);
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);

    // Calculation and result display
    switch (operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            if (num2 != 0)
                result = num1 / num2;
            else
                printf("Error! Division by zero.\n");
            return 1;
        default:
            printf("Error! Operator is not correct.\n");
            return 1;
    }

    printf("%.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);
    return 0;
}

1.2. File Reader

A program to read and display the contents of a file.

Features:

  • Opens a file specified by the user.

  • Reads its content and prints it to the screen.

  • Handles errors if the file cannot be opened.

Example Code:

#include <stdio.h>

int main() {
    FILE *file;
    char filename[100];
    char ch;

    printf("Enter the filename: ");
    scanf("%s", filename);

    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error! Could not open file.\n");
        return 1;
    }

    printf("Contents of %s:\n", filename);
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }

    fclose(file);
    return 0;
}

1.3. Number Guessing Game

A simple game where the user guesses a number between 1 and 100.

Features:

  • Generates a random number.

  • Allows the user to guess the number.

  • Provides feedback on whether the guess is too high or too low.

Example Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int number, guess, attempts = 0;

    // Initialize random seed
    srand(time(0));

    // Generate a random number between 1 and 100
    number = rand() % 100 + 1;

    // Game loop
    do {
        printf("Guess the number (1 to 100): ");
        scanf("%d", &guess);
        attempts++;

        if (guess > number)
            printf("Too high! Try again.\n");
        else if (guess < number)
            printf("Too low! Try again.\n");
        else
            printf("Congratulations! You guessed the number in %d attempts.\n", attempts);
    } while (guess != number);

    return 0;
}

2. Solving Real-World Problems

2.1. To-Do List Application

A program to manage a list of tasks.

Features:

  • Add tasks.

  • List tasks.

  • Mark tasks as completed.

Example Code:

This one can get a bit complex, so start with a simple version and expand as you become more comfortable with file handling and data structures.

2.2. Budget Tracker

A simple program to track income and expenses.

Features:

  • Add income and expenses.

  • Calculate and display the total balance.

Example Code:

#include <stdio.h>

int main() {
    float income = 0, expenses = 0, balance;

    printf("Enter your total income: ");
    scanf("%f", &income);

    printf("Enter your total expenses: ");
    scanf("%f", &expenses);

    balance = income - expenses;

    printf("Your balance is: %.2f\n", balance);

    return 0;
}

3. Practice Tips

  1. Start Small: Begin with simple projects and gradually move to more complex ones as you become more comfortable with the language.

  2. Read Documentation: Familiarize yourself with the C Standard Library functions and use them to make your code more efficient and powerful.

  3. Debug Regularly: Use GDB or other debugging tools to troubleshoot issues and understand how your code executes.

  4. Optimize Gradually: Focus on writing correct code first, then work on optimizing for performance and memory usage.

  5. Collaborate and Share: Share your projects with others, get feedback, and work on collaborative projects to learn from different perspectives.

PreviousDebugging & OptimizationNextCheatsheet

Last updated 9 months ago

Was this helpful?