Posted on • Edited on
C++, Preprocessor directives: #define, #ifdef...
Even though #define, #include, #ifdef, #ifndef, #endif.. are very common but when i try a hackerrank problem, the term "Preprocessor directives" sound unfamiliar to me. So i just summarize about it here.
Definition: Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements
Some types of preprocessor directives that I frequently use :
Macro Definition
#define foreach(v,i) for(int i = 0; i< v.size();i++)#define toStr(S) #S // to string#define getmax(a,b) ((a) > (b)) ? (a): (b)#define FUNCTION(name, operator) int name(int a, int b){if(a operator b)return a;else return b;}FUNCTION(maximum,>)FUNCTION(minimum,<)voidtestFunc(){vector<int>iV={3,5,7,8,3,5,4};foreach(iV,k){cout<<" "<<iV[k];}cout<<endl;inta=9;intb=52;intmin_val=minimum(a,b);intmax_val=getmax(a,b);cout<<"Debug info : "<<endl;cout<<" File : "<<__FILE__<<endl;//predefined macro for debugcout<<" Line : "<<__LINE__<<endl;cout<<endl;cout<<toStr(MinValue=)<<min_val<<endl;cout<<toStr(MaxValue=)<<max_val<<endl;}
File Inclusion :#include "header.h"
Conditional Compilation
#define DEBUGvoidtest_define(){#ifdef DEBUGcout<<"Debug mode enabled."<<std::endl;#elsecout<<" not define DEBUG "<<endl;#endif#ifndef RELEASEcout<<"Not in release mode."<<std::endl;#endif}
There are others directives like :
#undef#pragma startup#pragma exit...
but i never use except#pragma once
to prevent multiple inclusions of the file
Top comments(3)

- Email
- LocationSan Francisco Bay Area
- EducationUniversity of Illinois at Urbana-Champaign
- WorkRetired Principal Software Engineer
- Joined
Macro parameters should always be in parentheses:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
to prevent unintended expansions due to operator precedence.
There's a standard way to do debugging macros and it's:
#ifndef NDEBUG
Seeassert()
for details.
Your definition offoreach
should be:
#define FOR_EACH(E,C) for ( auto const &E : (C) )
but range-based for loops really eliminate the need for any such macro now.
Modern compilers really don't need#pragma once
any more.
You completely ignore some of the more interesting things about the preprocessor.
- EducationChosun University, South Korea
- WorkSoftware Engineer
- Joined
thank you very much for your advice

- LocationMunich, Germany
- EducationIt was so many years ago that now plays no role :)
- WorkCreator of https://github.com/aregtech/areg-sdk/ . This is for heart and the rest is for food :)
- Joined
Modern compilers really don't need
#pragma once
any more.
If you develop a library / framework, it is even better to use#ifndef ... #define ... #endif ...
. It guarantees that the same header in different location will be never included twice. In addition, developers can exclude obsolete / deprecated headers.
For further actions, you may consider blocking this person and/orreporting abuse