C

C Tutorial — Learn C Programming

Key takeaway: This free C tutorial teaches you systems programming from scratch — variables, pointers, arrays, functions, structs, and memory management — with practical code examples you can compile and run.

C is a general-purpose, procedural programming language created in 1972. It is used for operating systems, embedded systems, game engines, and performance-critical applications. This tutorial is for beginners, CS students, and developers who want to understand how computers work at a low level.

Last updated: September 2026

File Handling in C

File handling allows programs to store data permanently and read data from external files. C provides several functions for file operations through the stdio.h library.

Opening and Closing Files

Basic File Operations
#include <stdio.h>

int main() {
    FILE *file;
    
    // Open file for writing
    file = fopen("example.txt", "w");
    
    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }
    
    // Write to file
    fprintf(file, "Hello, World!\n");
    fprintf(file, "This is a test file.\n");
    
    // Close file
    fclose(file);
    
    printf("File written successfully.\n");
    
    return 0;
}

Reading from Files

File Reading Methods
#include <stdio.h>

int main() {
    FILE *file;
    char buffer[100];
    
    // Open file for reading
    file = fopen("example.txt", "r");
    
    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }
    
    printf("File contents:\n");
    
    // Method 1: Read line by line
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        printf("%s", buffer);
    }
    
    // Reset file pointer to beginning
    rewind(file);
    
    printf("\nReading character by character:\n");
    
    // Method 2: Read character by character
    int ch;
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }
    
    fclose(file);
    
    return 0;
}

File Modes

Different File Modes
#include <stdio.h>

int main() {
    FILE *file;
    
    // Write mode - creates new file or overwrites existing
    file = fopen("test1.txt", "w");
    fprintf(file, "Write mode\n");
    fclose(file);
    
    // Append mode - adds to end of existing file
    file = fopen("test1.txt", "a");
    fprintf(file, "Append mode\n");
    fclose(file);
    
    // Read mode - opens existing file for reading
    file = fopen("test1.txt", "r");
    char buffer[100];
    printf("File contents after append:\n");
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        printf("%s", buffer);
    }
    fclose(file);
    
    // Read and write mode
    file = fopen("test2.txt", "w+");
    fprintf(file, "Read and write mode\n");
    rewind(file);  // Go back to beginning
    fgets(buffer, sizeof(buffer), file);
    printf("Read back: %s", buffer);
    fclose(file);
    
    return 0;
}

Binary File Operations

Binary File Handling
#include <stdio.h>

struct Student {
    int id;
    char name[50];
    float gpa;
};

int main() {
    FILE *file;
    struct Student students[3] = {
        {1, "Alice", 3.8},
        {2, "Bob", 3.5},
        {3, "Charlie", 3.9}
    };
    
    // Write binary data
    file = fopen("students.dat", "wb");
    if (file == NULL) {
        printf("Error creating file!\n");
        return 1;
    }
    
    fwrite(students, sizeof(struct Student), 3, file);
    fclose(file);
    
    // Read binary data
    file = fopen("students.dat", "rb");
    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }
    
    struct Student readStudents[3];
    fread(readStudents, sizeof(struct Student), 3, file);
    fclose(file);
    
    printf("Students read from binary file:\n");
    for (int i = 0; i < 3; i++) {
        printf("ID: %d, Name: %s, GPA: %.1f\n",
               readStudents[i].id,
               readStudents[i].name,
               readStudents[i].gpa);
    }
    
    return 0;
}
File Mode Reference:
  • "r" - Read only
  • "w" - Write only (overwrites)
  • "a" - Append only
  • "r+" - Read and write
  • "w+" - Read and write (overwrites)
  • "a+" - Read and append
  • Add "b" for binary mode (e.g., "rb", "wb")

Frequently Asked Questions

C is a foundational systems programming language that's been around since the 1970s. It's the base for Linux, Windows, and most operating systems. Learning C gives you deep understanding of memory, pointers, and how computers work — skills that transfer to every other language. According to the TIOBE Index, C consistently ranks in the top 2 most popular languages.

No, zero experience needed. Our C programming tutorial is designed for complete beginners. We start with Hello World and gradually progress to pointers and memory management. Basic computer literacy and logical thinking are helpful but not required.

Just a C compiler — or use our free online compiler. For beginners, our online C compiler requires no installation. For local development, use GCC (available on all platforms). IDEs like VS Code, Code::Blocks, or Dev-C++ also work well.

2–4 weeks for basics, 3–6 months for proficiency. With 1–2 hours daily practice, you can learn syntax, loops, and functions in a few weeks. Mastering pointers, memory management, and data structures takes 6–12 months. Practice with our C programming examples.

C programming skills open doors to various career paths including system programming, embedded systems development, operating system development, device driver programming, game development, and firmware development. Many companies in automotive, aerospace, telecommunications, and IoT sectors actively seek C programmers.

Yes, extremely relevant. C consistently ranks in the top 2 on TIOBE and powers Linux, embedded systems, IoT, automotive software, and game engines. Understanding C makes you a better programmer in any language since most runtimes (Python, Java, Node.js) are themselves written in C.