Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

dinhluanbmt
dinhluanbmt

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;}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

There are others directives like :

#undef#pragma startup#pragma exit...
Enter fullscreen modeExit fullscreen mode

but i never use except#pragma once to prevent multiple inclusions of the file

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
pauljlucas profile image
Paul J. Lucas
C++ Jedi Master
  • Email
  • Location
    San Francisco Bay Area
  • Education
    University of Illinois at Urbana-Champaign
  • Work
    Retired Principal Software Engineer
  • Joined

Macro parameters should always be in parentheses:

#define MAX(a, b) ((a) > (b) ? (a) : (b))
Enter fullscreen modeExit fullscreen mode

to prevent unintended expansions due to operator precedence.

There's a standard way to do debugging macros and it's:

#ifndef NDEBUG
Enter fullscreen modeExit fullscreen mode

Seeassert() for details.

Your definition offoreachshould be:

#define FOR_EACH(E,C) for ( auto const &E : (C) )
Enter fullscreen modeExit fullscreen mode

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.

CollapseExpand
 
dinhluanbmt profile image
dinhluanbmt
I just wrote some notes based on my experience about C++. I hope this helps.
  • Education
    Chosun University, South Korea
  • Work
    Software Engineer
  • Joined

thank you very much for your advice

CollapseExpand
 
aregtech profile image
Artak Avetyan
I am a father, I am a husband and simply great guy :)Developing AREG cross-platform communication engine to automate real-time data transmission between connected Things. Please support at GitHub.
  • Location
    Munich, Germany
  • Education
    It was so many years ago that now plays no role :)
  • Work
    Creator 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.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I just wrote some notes based on my experience about C++. I hope this helps.
  • Education
    Chosun University, South Korea
  • Work
    Software Engineer
  • Joined

More fromdinhluanbmt

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp