Header Files and Modular C Programs Practice Questions with Solutions

Introduction

Header files help organize C programs by keeping declarations, macros, and reusable definitions separate from the main program. Modular programming takes this idea further by dividing a large program into multiple .c and .h files. In this chapter, you will practice creating custom header files, using #include, sharing functions between files, and building simple modular C programs. Header Files and Modular C Programs practice questions with solutions to help you understand the concepts.

Q1. Use a Standard Header File

Problem Statement

Write a C program that uses the stdio.h header file to print a message.

C Program

#include <stdio.h>

int main()
{
    printf("Hello, C Programming!");

    return 0;
}

Sample Output

Hello, C Programming!

Explanation

This line:

#include <stdio.h>

includes the standard input/output header file.

It provides declarations for functions such as:

printf()
scanf()

The angle brackets:

<stdio.h>

are commonly used for standard library headers.

Concepts Covered

  • Header files
  • #include
  • stdio.h
  • Standard library

Q2. Create Your First Custom Header File

Problem Statement

Create a custom header file named message.h containing a function declaration, and use that function in the main C program.

File 1: message.h

#ifndef MESSAGE_H
#define MESSAGE_H

void showMessage();

#endif

File 2: message.c

#include <stdio.h>
#include "message.h"

void showMessage()
{
    printf("Welcome to C Programming!");
}

File 3: main.c

#include "message.h"

int main()
{
    showMessage();

    return 0;
}

How to Compile

With GCC:

gcc main.c message.c -o program

Run:

program

Sample Output

Welcome to C Programming!

Explanation

We have divided the program into three files.

message.h contains the function declaration:

void showMessage();

message.c contains the actual function definition.

main.c calls the function.

The custom header is included using:

#include "message.h"

Double quotation marks are commonly used for user-created headers.

Concepts Covered

  • Custom header files
  • Function declaration
  • Function definition
  • Multiple C files
  • #include

Q3. Create a Header File for Addition

Problem Statement

Create a custom header file containing an addition function and use it from another C file.

File 1: math.h

#ifndef MATH_H
#define MATH_H

int add(int a, int b);

#endif

File 2: math.c

#include "math.h"

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

File 3: main.c

#include <stdio.h>
#include "math.h"

int main()
{
    int result;

    result = add(20, 30);

    printf("Sum = %d", result);

    return 0;
}

How to Compile

gcc main.c math.c -o program

Sample Output

Sum = 50

Explanation

The header file contains the declaration:

int add(int a, int b);

The actual function is written in math.c.

The main program only needs to include:

#include "math.h"

This keeps the program organized.

Concepts Covered

  • Custom header
  • Function prototype
  • Multiple source files
  • Modular programming

Q4. Create a Header for Multiple Mathematical Functions

Problem Statement

Create a custom header file containing declarations for addition, subtraction, multiplication, and division functions.

File 1: calculator.h

#ifndef CALCULATOR_H
#define CALCULATOR_H

int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
float divide(int a, int b);

#endif

File 2: calculator.c

#include "calculator.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(int a, int b)
{
    return (float)a / b;
}

File 3: main.c

#include <stdio.h>
#include "calculator.h"

int main()
{
    printf("Addition = %d\n", add(20, 10));
    printf("Subtraction = %d\n", subtract(20, 10));
    printf("Multiplication = %d\n", multiply(20, 10));
    printf("Division = %.2f\n", divide(20, 10));

    return 0;
}

How to Compile

gcc main.c calculator.c -o calculator

Sample Output

Addition = 30
Subtraction = 10
Multiplication = 200
Division = 2.00

Explanation

The header file contains the declarations of all calculator functions.

The implementation is kept inside calculator.c.

The main program only uses the functions.

This is a simple example of separating interface from implementation.

Concepts Covered

  • Multiple function declarations
  • Custom header files
  • Modular programming
  • Source files

Q5. Create a Student Utility Module

Problem Statement

Create a separate student module containing a function that calculates the average of three marks.

File 1: student.h

#ifndef STUDENT_H
#define STUDENT_H

float calculateAverage(int m1, int m2, int m3);

#endif

File 2: student.c

#include "student.h"

float calculateAverage(int m1, int m2, int m3)
{
    return (m1 + m2 + m3) / 3.0;
}

File 3: main.c

#include <stdio.h>
#include "student.h"

int main()
{
    int marks1 = 80;
    int marks2 = 75;
    int marks3 = 90;

    float average;

    average = calculateAverage(marks1, marks2, marks3);

    printf("Average Marks = %.2f", average);

    return 0;
}

How to Compile

gcc main.c student.c -o student

Sample Output

Average Marks = 81.67

Explanation

The student-related calculation is placed in a separate module.

student.h tells the compiler about the function:

float calculateAverage(int m1, int m2, int m3);

student.c contains the implementation.

main.c uses the function.

Concepts Covered

  • Modular programming
  • Header files
  • Function declaration
  • Floating-point calculations

Q6. Use a Header File for Macros

Problem Statement

Create a header file containing mathematical macros and use those macros in a separate C program.

File 1: macros.h

#ifndef MACROS_H
#define MACROS_H

#define SQUARE(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))

#endif

File 2: main.c

#include <stdio.h>
#include "macros.h"

int main()
{
    int number = 5;

    printf("Square = %d\n", SQUARE(number));
    printf("Cube = %d", CUBE(number));

    return 0;
}

How to Compile

gcc main.c -o program

Sample Output

Square = 25
Cube = 125

Explanation

The header file contains reusable macros:

#define SQUARE(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))

The main program gets access to them using:

#include "macros.h"

This keeps reusable definitions separate from the main program.

Concepts Covered

  • Header files
  • Macros
  • #define
  • Custom modules

Q7. Build a Simple Calculator Using Three Files

Problem Statement

Create a modular calculator using:

  • calculator.h
  • calculator.c
  • main.c

The program should calculate the sum and product of two numbers.

File 1: calculator.h

#ifndef CALCULATOR_H
#define CALCULATOR_H

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

#endif

File 2: calculator.c

#include "calculator.h"

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

int multiply(int a, int b)
{
    return a * b;
}

File 3: main.c

#include <stdio.h>
#include "calculator.h"

int main()
{
    int a = 12;
    int b = 5;

    printf("Sum = %d\n", add(a, b));
    printf("Product = %d", multiply(a, b));

    return 0;
}

How to Compile

gcc main.c calculator.c -o calculator

Sample Output

Sum = 17
Product = 60

Explanation

The program is divided into separate responsibilities:

calculator.h
     ↓
Function declarations

calculator.c
     ↓
Function definitions

main.c
     ↓
Program execution

This structure becomes very useful when programs become larger.

Concepts Covered

  • Modular programming
  • Header files
  • Function declarations
  • Function definitions
  • Multiple source files

Q8. Use Include Guards in a Header File

Problem Statement

Create a header file with an include guard so that its contents are not processed more than once.

File: constants.h

#ifndef CONSTANTS_H
#define CONSTANTS_H

#define MAX_STUDENTS 100
#define PASS_MARKS 40

#endif

File: main.c

#include <stdio.h>
#include "constants.h"

int main()
{
    printf("Maximum Students = %d\n", MAX_STUDENTS);
    printf("Passing Marks = %d", PASS_MARKS);

    return 0;
}

Sample Output

Maximum Students = 100
Passing Marks = 40

Explanation

These lines:

#ifndef CONSTANTS_H
#define CONSTANTS_H

...

#endif

are called an include guard.

The preprocessor checks whether CONSTANTS_H has already been defined.

If it has not, the contents are processed and CONSTANTS_H is defined.

This helps prevent problems caused by including the same header multiple times.

Concepts Covered

  • Include guards
  • #ifndef
  • #define
  • #endif
  • Header protection

Q9. Create Separate Modules for Student Marks and Results

Problem Statement

Create a modular program with separate files for calculating total marks and percentage.

File 1: result.h

#ifndef RESULT_H
#define RESULT_H

int calculateTotal(int a, int b, int c);
float calculatePercentage(int total);

#endif

File 2: result.c

#include "result.h"

int calculateTotal(int a, int b, int c)
{
    return a + b + c;
}

float calculatePercentage(int total)
{
    return total / 3.0;
}

File 3: main.c

#include <stdio.h>
#include "result.h"

int main()
{
    int total;
    float percentage;

    total = calculateTotal(80, 70, 90);
    percentage = calculatePercentage(total);

    printf("Total = %d\n", total);
    printf("Percentage = %.2f%%", percentage);

    return 0;
}

How to Compile

gcc main.c result.c -o result

Sample Output

Total = 240
Percentage = 80.00%

Explanation

The program separates result-related operations from the main program.

result.h contains declarations.

result.c contains calculations.

main.c controls the program.

This is a practical example of organizing code into a reusable module.

Concepts Covered

  • Modular programming
  • Header files
  • Function declarations
  • Function definitions
  • Multiple source files

Q10. Build a Complete Modular C Program

Problem Statement

Create a simple modular C program that calculates the area and perimeter of a rectangle using separate header and source files.

File 1: rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

int calculateArea(int length, int width);
int calculatePerimeter(int length, int width);

#endif

File 2: rectangle.c

#include "rectangle.h"

int calculateArea(int length, int width)
{
    return length * width;
}

int calculatePerimeter(int length, int width)
{
    return 2 * (length + width);
}

File 3: main.c

#include <stdio.h>
#include "rectangle.h"

int main()
{
    int length = 10;
    int width = 5;

    printf("Length = %d\n", length);
    printf("Width = %d\n", width);

    printf("Area = %d\n", calculateArea(length, width));
    printf("Perimeter = %d", calculatePerimeter(length, width));

    return 0;
}

How to Compile

gcc main.c rectangle.c -o rectangle

Sample Output

Length = 10
Width = 5
Area = 50
Perimeter = 30

Explanation

This program follows a clean modular structure:

rectangle.h
    ↓
Declarations

rectangle.c
    ↓
Rectangle calculations

main.c
    ↓
Program execution

If you later want to change the rectangle calculations, you can modify rectangle.c without putting all the calculation code inside main.c.

This is one of the main benefits of modular programming.

Concepts Covered

  • Custom header files
  • Include guards
  • Multiple source files
  • Function declarations
  • Function definitions
  • Modular programming

Key Takeaways

  • Header files normally use the .h extension.
  • Source files normally use the .c extension.
  • Header files commonly contain declarations, macros, constants, and type definitions.
  • Source files contain function implementations and program logic.
  • Custom headers are commonly included with double quotes.
  • Standard headers are commonly included with angle brackets.
  • Include guards help prevent a header from being processed multiple times.
  • Modular programming divides a large program into smaller files.
  • Multiple .c files can be compiled together.
  • A header provides an interface that other source files can use.
  • Keeping declarations and implementations organized makes larger C projects easier to manage.

FAQs

1. What is a header file in C?

A header file is a file, usually ending in .h, that contains declarations, macros, constants, type definitions, or other information that can be shared between C source files.

2. What is the difference between .h and .c files?

A .h file commonly contains declarations, while a .c file commonly contains function implementations and program logic.

3. How do I create a custom header file in C?

Create a file such as:

calculator.h

Then place declarations inside it:

int add(int a, int b);

Include it in your C program with:

#include "calculator.h"

4. Why are include guards used in C?

Include guards prevent the contents of a header file from being processed multiple times in the same compilation unit.

5. What is modular programming in C?

Modular programming means dividing a program into smaller, organized modules, usually using multiple .c and .h files.

6. Can a C program have multiple .c files?

Yes. A C project can contain many source files. They can be compiled and linked together to create one executable program.

7. Why should I use modular programming?

Modular programming helps separate different parts of a project, making larger programs easier to read, maintain, test, and reuse.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top