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
#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
#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
#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
#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")