Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

C Header Files

What are Header Files?

A header file (.h) is a file containing declarations — function prototypes, macros, constants, and type definitions — that can be shared across multiple .c source files. They are the C way of creating reusable interfaces.

  • System headers — provided by the C standard library: <stdio.h>, <stdlib.h>, <string.h>, etc.
  • Custom headers — created by you to organize your own code.
SyntaxMeaning
#include <stdio.h>System header — searched in compiler's include path
#include "myheader.h"Custom header — searched in current directory first

Common Standard Library Headers

HeaderPurposeKey Functions
<stdio.h>Input/Outputprintf, scanf, fopen, fclose, fprintf
<stdlib.h>General utilitiesmalloc, free, atoi, exit, rand, qsort
<string.h>String operationsstrlen, strcpy, strcat, strcmp, memcpy
<math.h>Math functionssqrt, pow, sin, cos, floor, ceil, fabs
<time.h>Date and timetime, clock, difftime, strftime
<ctype.h>Character classificationisalpha, isdigit, toupper, tolower
<errno.h>Error codeserrno, perror, strerror
<limits.h>Type limitsINT_MAX, INT_MIN, CHAR_MAX, LONG_MAX
<stdbool.h>Boolean type (C99)bool, true, false
<stdint.h>Fixed-width integersint8_t, uint32_t, int64_t

Creating a Custom Header File

The key rule: always use include guards (or #pragma once) to prevent a header from being included multiple times in the same translation unit.

Custom Header — mathutils.h + mathutils.c + main.c
// mathutils.h — declarations only (no implementation)

#ifndef MATHUTILS_H   // include guard: if not already defined...
#define MATHUTILS_H   // ...define it (prevents double inclusion)

// Constants
#define PI 3.14159265358979

// Function prototypes (declarations)
int    add(int a, int b);
int    subtract(int a, int b);
double circleArea(double radius);
int    isPrime(int n);

#endif  // MATHUTILS_H
// mathutils.c — implementations
#include "mathutils.h"  // include our own header

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

double circleArea(double radius) {
    return PI * radius * radius;
}

int isPrime(int n) {
    if (n < 2) return 0;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}
// main.c — uses the mathutils module
#include <stdio.h>
#include "mathutils.h"  // our custom header

int main() {
    printf("add(3, 4)       = %d\n",   add(3, 4));
    printf("subtract(10, 3) = %d\n",   subtract(10, 3));
    printf("circleArea(5.0) = %.2f\n", circleArea(5.0));
    printf("isPrime(17)     = %d\n",   isPrime(17));
    printf("isPrime(18)     = %d\n",   isPrime(18));
    printf("PI              = %.5f\n", PI);
    return 0;
}

// Compile: gcc main.c mathutils.c -o app
// Output:
// add(3, 4)       = 7
// subtract(10, 3) = 7
// circleArea(5.0) = 78.54
// isPrime(17)     = 1
// isPrime(18)     = 0
// PI              = 3.14159

Include Guards vs #pragma once

MethodSyntaxPortabilityNotes
Include guards#ifndef / #define / #endifStandard C — works everywhereVerbose but guaranteed
#pragma once#pragma onceSupported by GCC, Clang, MSVCSimpler, not in C standard

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.