QP School

Full Version: Creating and using header files in C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Header files contain function prototypes and other declarations.

// math_operations.h
#ifndef MATH_OPERATIONS_H
#define MATH_OPERATIONS_H

int add(int a, int b);
int subtract(int a, int b);

#endif

// math_operations.c
#include "math_operations.h"

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

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

// main.c
#include <stdio.h>
#include "math_operations.h"

int main() {
    int result = add(10, 5);
    printf("Addition: %d\n", result); // Output: 15

    result = subtract(10, 5);
    printf("Subtraction: %d\n", result); // Output: 5
    return 0;
}