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
Header Files in C
Header files contain function declarations, macro definitions, and type definitions that can be shared across multiple source files. They promote code reusability and organization.
Creating Custom Header Files
// math_utils.h
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
// Function declarations
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
float divide(float a, float b);
int power(int base, int exponent);
// Constants
#define PI 3.14159265359
#define E 2.71828182846
// Macro functions
#define SQUARE(x) ((x) * (x))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#endif // MATH_UTILS_H// math_utils.c
#include "math_utils.h"
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
float divide(float a, float b) {
if (b != 0) {
return a / b;
}
return 0.0; // Error case
}
int power(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}// main.c
#include <stdio.h>
#include "math_utils.h"
int main() {
int a = 10, b = 5;
printf("Addition: %d + %d = %d\n", a, b, add(a, b));
printf("Subtraction: %d - %d = %d\n", a, b, subtract(a, b));
printf("Multiplication: %d * %d = %d\n", a, b, multiply(a, b));
printf("Division: %d / %d = %.2f\n", a, b, divide(a, b));
printf("Power: %d^%d = %d\n", a, 3, power(a, 3));
printf("\nUsing macros:\n");
printf("PI = %.5f\n", PI);
printf("Square of %d = %d\n", a, SQUARE(a));
printf("Absolute value of -15 = %d\n", ABS(-15));
return 0;
}Header Guards
// student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <stdio.h>
typedef struct {
int id;
char name[50];
float gpa;
} Student;
// Function prototypes
void printStudent(const Student *s);
void initStudent(Student *s, int id, const char *name, float gpa);
int compareStudents(const Student *s1, const Student *s2);
#endif // STUDENT_H
// Alternative: #pragma once (compiler-specific)
// #pragma onceStandard Library Headers
#include <stdio.h> // Standard I/O functions
#include <stdlib.h> // Memory allocation, conversion
#include <string.h> // String manipulation functions
#include <math.h> // Mathematical functions
#include <time.h> // Date and time functions
#include <ctype.h> // Character classification
#include <limits.h> // Implementation limits
#include <float.h> // Floating-point limits
int main() {
// stdio.h functions
printf("Hello from stdio.h\n");
// stdlib.h functions
int *ptr = malloc(sizeof(int) * 5);
free(ptr);
// string.h functions
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, " ");
strcat(str1, str2);
printf("Concatenated: %s\n", str1);
// math.h functions (compile with -lm)
printf("Square root of 16: %.2f\n", sqrt(16.0));
printf("Power 2^3: %.0f\n", pow(2.0, 3.0));
// time.h functions
time_t current_time = time(NULL);
printf("Current time: %s", ctime(¤t_time));
// limits.h constants
printf("Maximum int value: %d\n", INT_MAX);
printf("Minimum int value: %d\n", INT_MIN);
return 0;
}Compilation with Multiple Files
# Compile all files together
gcc main.c math_utils.c -o program
# Compile separately and link
gcc -c main.c # Creates main.o
gcc -c math_utils.c # Creates math_utils.o
gcc main.o math_utils.o -o program
# With math library
gcc main.c math_utils.c -lm -o programHeader File Best Practices:
- Always use header guards or #pragma once
- Include only necessary headers
- Use angle brackets <> for system headers
- Use quotes "" for user-defined headers
- Declare functions, don't define them in headers