What the heck is "Preprocessor directive" in C++?
Exploring the Role of Preprocessor Directives in C++ Programming
Table of contents
- The goal is to share: what is Preprocessor Directive in C & C++ and what are the different ways we use it.
- So why is it written in the very first line of code, and what does it do to my code? and what will happen if I don’t write this? So many questions, right?
- Suppose you want to write a program in C++ that clears screen on different OS, here you can achive by single program using “Pre-processor directive”:
The goal is to share: what is Preprocessor Directive in C & C++ and what are the different ways we use it.
I’m writing this ‘blog’ today 2 February 2025 because I found ‘preprocessor directive’ fascinating way in C++ to do before compilation of code. I’m listening to the song The Journey by Tom Misch
You’ve written this below code snippet, whenever you had chance to write C or C++ program in your school, high-school or uni.
#include<iostream>
using namespace std;
int main(){
cout<< "Welcome to Ashish's blog" <<endl;
return 0;
}
On 29 Jan 2025 I started my C++ journey till then I was learning Java and loved Java so much but had to leave, and when I wrote my very first line code “#include<iostream>”, then I was like what the hell is this and why there is #, can’t it be other symbol than #? at that time my curiosity was at its peak.
I Googled, did ChatGPT, then got to know that there is no single thing # only, it comes with #include and this “#include” is called ‘preprocessor directive’ in C and C++ and there are many types of it. Like to define micro, you write: #define MAX(a,b) ( a > b ? a : b); here #define is also a pre-processor directive.
So why is it written in the very first line of code, and what does it do to my code? and what will happen if I don’t write this? So many questions, right?
These were the questions in my mind back then. So first look at the diagram below:
Do you get anything? a little bit IMO. This is how our code go from a High-level to low level.
So when you write “#include …”, you just told to pre-processor to perform certain action, in this case you’re saying to pre-processor bhai kuchh to lele apne sath me, may be kam aa jaye future me (bro as pre-processor include something that may be we require in the future). Basically we’re giving some instructions to pre-processor to do some action before compile our source file, it can of many types:
To include some files or headers. (user-defined headers as well)
To run a particular part of code of our program. (Conditional compilation of code)
To guard our file from multiple inclusion of same headers.
To add micro in our program.
So, got the answer why is it written? To perform certain task before actual compilation of code because may be some part of our code is dependent on other files or standard C or C++ library, and to include them we give instructions to pre-processor.
#include<iostream>
In this case we’re using standard C++ way of taking input and displaying output, but we can also define how are we going to take input eg: from CSV file, spreadsheet, web application etc. and can display output on Refrigerator’s screen, washing machine screen etc. and then we’ll use our header like this:
#include "ioashish"
and you’ll get output on your washing machine’s screen.😎
and if we don’t write this, from where are we going to take input and where to display output? to do so, we include C++ standard I/O header file.
Why there is “#” to start with? because whoever is created the C and C++ language, they set the way to write pre-processor directive, if you don’t like it that’s fine, you can also add any sysmbol as your liking but you’ve to write C++ lang again, and you can do it I’m sure. but why want to do this, ask this question before doing it? (or before anything doing).
Suppose you want to write a program in C++ that clears screen on different OS, here you can achive by single program using “Pre-processor directive”:
#include<iostream>
#ifdef __WIN32
#include<window.h>
void clearScreen(){
system("cls");
}
#elif __linux__
#include<cstdlib>
void clearScreen(){
system("clear");
}
#else
void clearScreen(){
std::cout<< "Screen can't be clear on this OS. Thank you!"<<std::ndl;
}
#endif
int main(){
clearScreen();
return 0;
}
That’s from myside guys. au revoir 👋
Hero image credit: Discord blog
Gonna sooooon write on pre-processor.