---
title: "Macro expansion and file inclusion"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:macro-expansion-file-inclusion"
source: https://www.bctengineer.com/q/semester_1/computer_programming/macro-expansion-file-inclusion
site: BCT Engineering
---

# Macro expansion and file inclusion

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2074 Chaitra, 4 marks
- Video: [9  Preprocessors in C Macros and File inclusion](https://www.youtube.com/watch?v=-GnQdpYcsP0) — Codentertainment
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/macro-expansion-file-inclusion
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

What do you mean by macro expansion and file inclusion in C? Explain with example.

## Short answer

Macro expansion is the preprocessor replacing a #define name with its body text before compilation (e.g., #define PI 3.1416). File inclusion inserts the contents of another file via #include; <file.h> searches system directories, "file.h" searches current directory first.

## Answer

#### Macro expansion
A **macro** is a name defined with `#define` that stands for a piece of text. Before compilation the preprocessor replaces every use of the macro by its text. This is called macro expansion. A macro can be a simple constant (`#define PI 3.1416`) or take arguments (`#define SQUARE(x) ((x) * (x))`), so `SQUARE(r)` expands to `((r) * (r))`.

#### File inclusion
The `#include` directive inserts the contents of another file into the program at that place before compilation. `#include <stdio.h>` searches the standard system directories; `#include "mymath.h"` searches the current folder first. It is used for header files and for the programmer's own reusable code.

#### Example
File `mymath.h`:
```c
int cube(int n) { return n * n * n; }
```
Main program:
```c
#include <stdio.h>
#include "mymath.h"
#define PI 3.1416
#define SQUARE(x) ((x) * (x))
int main(void)
{
    float r = 2.0f;
    printf("Area = %.2f\n", PI * SQUARE(r));
    printf("Cube of 3 = %d\n", cube(3));
    return 0;
}
```

Output:
```text
Area = 12.57
Cube of 3 = 27
```

Here `PI` and `SQUARE(r)` are expanded as macros, and `mymath.h` is included into the program.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is macro expansion?
   A. The preprocessor replaces each macro use by its defined text
   B. The compiler allocates memory for macros
   C. The linker joins object files
   D. A function is called at run time
   Answer: A. Before compilation the preprocessor replaces macro names with their text. (Key point: Macro expansion)

2. SQUARE(r) with #define SQUARE(x) ((x) * (x)) expands to:
   A. r*2
   B. ((r) * (r))
   C. (x)*(x)
   D. r^2
   Answer: B. The argument replaces x in the macro text. (Key point: Macro with argument)

3. What does #include do?
   A. Runs another program
   B. Defines a macro
   C. Inserts the contents of another file at that place before compilation
   D. Deletes the header
   Answer: C. The preprocessor inserts the file contents. (Key point: File inclusion)

4. How does #include "mymath.h" differ from #include <stdio.h>?
   A. Quotes search the current folder first; angle brackets search standard directories
   B. They are identical
   C. Quotes are only for .c files
   D. Angle brackets search the current folder first
   Answer: A. Quotes look in the current folder first; angle brackets use system directories. (Key point: Quotes vs angle)

## Related in Computer Programming

- [Opening and closing a data file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aopen-close-file) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [Second quarterly progress report: road expansion](https://www.bctengineer.com/question.md?key=semester_5%2Fcommunication_english%3Aquarterly-progress-report-road-expansion) — Communication English, chapter 7
- [Generation expansion planning and load forecasting](https://www.bctengineer.com/question.md?key=semester_8%2Fpower_plant_design%3Ageneration-expansion-load-forecast) — Power Plant Design, chapter 2
- [Stream and file stream classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astream-file-classes) — Object Oriented Programming, chapter 8
- [Engineer opens confidential salary file](https://www.bctengineer.com/question.md?key=semester_8%2Fengineering_professional_practice%3Aconfidential-salary-file-conduct) — Engineering Professional Practice, chapter 7
