KwickAcademy C++ · 7 min · free
The C Preprocessor: Macros and Conditional Compilation
The C preprocessor edits your code as text before compiling: it pastes headers, replaces macros and keeps or drops code.
On screen in this lesson
What the preprocessor does
| Handles lines that start with # |
| Pastes header files into your code |
| Replaces macro names with their text |
| Removes comments |
| Keeps or drops code by conditions |
Two ways to #include
| Form | Searches | Used for |
|---|---|---|
| <stdio.h> | system folders | library headers |
| "marks.h" | your folder first | your own headers |
Rules for #define
| No = sign and no semicolon |
| Capital names by convention |
| Pure text replacement, no data type |
| Change the value in one place only |
Macro vs function
| Point | Macro | Function |
|---|---|---|
| Handled by | preprocessor | compiler |
| Type checking | none | yes |
| Call overhead | none | small |
| Brackets trap | yes | no |
Conditional directives
| Directive | Keeps code if | Example use |
|---|---|---|
| #if, #elif | a condition is true | choose a version |
| #ifdef | name is defined | debug messages |
| #ifndef | name not defined | header guard |
| #undef | removes a macro | reset a name |
Quick recap
| Preprocessor edits code before compiling |
| #include pastes a header file |
| #define makes a macro; no semicolon |
| Put brackets around macro arguments |
| #if, #ifdef, #ifndef keep or drop code |
Quick answers
Why does SQ(2 + 3) print 11 with #define SQ(x) x * x?
It pastes text: 2 + 3 * 2 + 3 is 11. Write ((x) * (x)).
What does a header guard stop?
A header file being included twice.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What runs first, preprocessor or compiler?41 sec
Should #define end with a semicolon?42 sec
What should SQ be?41 sec
When is code kept or dropped?43 secThe full lesson, in text
Hello students, welcome to Kwickprep. What is two plus three, squared? Twenty five, of course. But one small C macro can print eleven instead. Today we will learn what happens to your program before the compiler even sees it. We will cover include, define, macros with arguments, and conditional compilation, and we will solve the eleven mystery.
Here is the journey of a C program. It begins as a source file, the code you type, like prog dot c. First, the preprocessor reads it and makes text changes. Then the compiler turns that changed code into machine code. Then the linker joins it with library code, such as printf. The result is a program you can run.
The preprocessor is a tool that edits your code as text, before compiling. It handles lines that start with a hash sign, called directives, which are instructions to the preprocessor. It pastes the contents of header files into your code. It replaces each macro name with its text. It removes comments, since the compiler does not need them. And it can keep or drop parts of the code based on conditions.
The include directive copies a whole header file into your program. A header file, ending in dot h, holds declarations of functions and constants. With angle brackets, the preprocessor searches the system folders, so we use it for library headers like stdio dot h. With double quotes, it searches your project folder first, so we use it for headers we write ourselves.
The define directive creates a macro, which is a name that stands for some text. Here PI stands for three point one four, and SEATS stands for forty, the seats in a classroom. Before compiling, every PI is replaced with three point one four. So PI times two becomes three point one four times two. It prints six point two eight and forty.
Follow these rules for define. Write no equals sign and no semicolon, because the semicolon would be pasted into your code too. By convention, macro names are written in capital letters, so they stand out. A macro is pure text replacement, so it has no data type and uses no memory as a variable. The big benefit is that you change the value in one place, and the whole program updates.
A macro can take arguments, like a small function. SQUARE of x becomes x times x, so SQUARE of four gives sixteen. MAX of a and b uses the conditional operator, a question mark and a colon. It means, if a is greater than b, pick a, otherwise pick b. So MAX of seven and nine gives nine. Notice there is no space between the macro name and the opening bracket.
Now the mystery from the start. Pause the video and predict what this prints. Most students say twenty five, but it prints eleven. The macro pastes text without brackets, so it becomes two plus three times two plus three. Multiplication happens first, so three times two is six, and two plus six plus three is eleven. That is why we put brackets around every argument, and around the whole macro.
Exams often ask how a macro differs from a function. A macro is handled by the preprocessor, but a function is handled by the compiler. A macro has no type checking, while a function checks the types of its arguments. A macro has no call overhead, which means no time spent jumping to a function and back. But macros can fall into the brackets trap we just saw, and functions cannot.
Conditional compilation means the preprocessor keeps or removes code based on a condition. Here DEBUG is defined, with no value. The directive if def asks, is DEBUG defined? Yes, so the line Checking marks is kept. If we delete the define line, that printf is removed before compiling, as if we never wrote it. Programmers use this to switch extra checking messages on and off.
The if directive works like an if statement, but it runs before compiling. It can only test constant values, such as a macro. MARKS is forty, which is thirty three or more. So RESULT is defined as Pass, and the else part is thrown away. Every if block must be closed with end if.
Here are the conditional directives in one table. Hash if and hash elif keep code when a constant condition is true. Hash if def keeps code when a name is defined. Hash if n def keeps code when a name is not defined, and it is used in header guards. Hash undef removes a macro, so the name is no longer defined.
If a header file is included twice, the compiler may complain about repeated definitions. A header guard stops this. The first time, MARKS underscore H is not defined, so the code is kept and the name gets defined. The second time, the name already exists, so everything up to end if is skipped.
Let us revise what we learned today. The preprocessor edits your code as text before the compiler runs. Include pastes a header file into your program. Define makes a macro, with no equals sign and no semicolon. Always put brackets around macro arguments, or you may get eleven instead of twenty five. And if, if def and if n def keep or drop code before compiling.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 10 Information Technology (402) | Electronic Spreadsheet (Advanced) using LibreOffice Calc |
| Programming All levels C | File Handling and the Preprocessor |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

