Intriguing App Examples in C Programming: A Deep Dive

C programming is a versatile and powerful language that can be used to create a wide range of applications. In this blog post, we will explore some intriguing app examples in C programming and learn how to build them. These examples will not only enhance your C programming skills but also broaden your programming horizons.

Table of Contents

  1. Creating a Simple Calculator
  2. Building a Command-Line File Explorer
  3. Developing a Basic Text Editor
  4. Implementing a Simple Web Server
  5. Designing a Basic Game Engine
  6. Conclusion

Creating a Simple Calculator

A simple calculator is a beginner-friendly app example that takes basic arithmetic inputs and returns the result. It helps in understanding the concepts of input, processing, and output in C programming.

#include <stdio.h>

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

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

    switch(operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            result = num1 / num2;
            break;
        default:
            printf("Invalid operator!");
    }

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

Building a Command-Line File Explorer

A command-line file explorer allows users to navigate through directories, list files, and perform basic file operations. It is an excellent example of using C's file I/O and directory handling functions.

#include <stdio.h>
#include <dirent.h>

int main(int argc, char *argv[]) {
    DIR *dir;
    struct dirent *entry;

    if (argc < 2) {
        printf("Usage: %s <directory>\n", argv[0]);
        return 1;
    }

    dir = opendir(argv[1]);
    if (!dir) {
        perror("opendir");
        return 1;
    }

    while ((entry = readdir(dir))) {
        printf("%s\n", entry->d_name);
    }

    closedir(dir);
    return 0;
}

Developing a Basic Text Editor

A basic text editor allows users to create, edit, and save text files. It is a more advanced example that demonstrates the use of dynamic memory allocation, file I/O, and string manipulation in C programming.

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

int main() {
    char filename[100], line[1000];
    FILE *file;
    int choice;

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

    printf("1. Write\n2. Read\nEnter your choice: ");
    scanf("%d", &choice);

    switch(choice) {
        case 1:
            file = fopen(filename, "w");
            if (!file) {
                perror("fopen");
                return 1;
            }
            printf("Enter text (end with a . on a new line):\n");
            while (1) {
                fgets(line, 1000, stdin);
                if (strcmp(line, ".\n") == 0) break;
                fputs(line, file);
            }
            fclose(file);
            break;
        case 2:
            file = fopen(filename, "r");
            if (!file) {
                perror("fopen");
                return 1;
            }
            while (fgets(line, 1000, file)) {
                printf("%s", line);
            }
            fclose(file);
            break;
        default:
            printf("Invalid choice!");
    }

    return 0;
}

Implementing a Simple Web Server

A simple web server that listens for HTTP requests and serves static files is a more advanced example of using C programming for socket programming and network communication.

// This example requires the use of a POSIX-compliant operating system.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 8080

int main() {
    int server_fd, client_fd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    char buffer[1024];

    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd == -1) {
        perror("socket");
        return 1;
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));

    if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("bind");
        return 1;
    }

    if (listen(server_fd, 10) == -1) {
        perror("listen");
        return 1;
    }

    while (1) {
        printf("Waiting for a connection...\n");
        client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
        if (client_fd == -1) {
            perror("accept");
            continue;
        }

        printf("Connected to client\n");
        // Implement the logic to read HTTP request and serve files.

        close(client_fd);
    }

    close(server_fd);
    return 0;
}

Designing a Basic Game Engine

A basic game engine involves graphics, user input, and game logic. It is an excellent example to learn about graphical libraries, event handling, and game development concepts in C programming.

// This example requires the installation of the SDL2 library.
#include <SDL2/SDL.h>

int main(int argc, char *argv[]) {
    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Event event;
    int is_running = 1;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
        return 1;
    }

    window = SDL_CreateWindow("Basic Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    if (!window) {
        fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError());
        return 1;
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (!renderer) {
        fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError());
        return 1;
    }

    while (is_running) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                is_running = 0;
            }
        }

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        // Implement game logic and rendering here.

        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

Conclusion

These intriguing app examples in C programming serve as a foundation for building more complex and feature-rich applications. As you delve into these examples, you will gain a deeper understanding of C programming concepts and strengthen your skills, ultimately expanding your programming repertoire.

An AI coworker, not just a copilot

View VelocityAI