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

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
// 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 (Implementation)
// 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 (Using the Header)
// 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

Preventing Multiple Inclusions
// 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 once

Standard Library Headers

Common Standard 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

Compilation Commands
# 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 program
Header 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

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.