ifdef #ifndef for ONLINE_JUDGE in C++

Revision en5, by KR1shna, 2020-03-30 13:47:26

It might be useful to some of us
Introduction:
Before a C++ program gets compiled by the compiler, the source-code gets processed by the compiler. This technique is called preprocessor and the process is called preprocessing.
It is separate program part that the C++ compiler invokes it at the first part of the translation. This technique is not a part of the compiler but it is a separate method that comes under compilation process. It directs the compiler that the information should be preprocessed before the actual compilation starts.
All preprocessor directives in C++ begin with #, and they do not need to end with a semicolon(;) because this is not a statement in C++.
Imagw
Look at the following piece of code in C++:

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define macro1
#define macro2
...
#include "local_header1.h"
#include "local_header2.h"
...
#endif
//.....//


#ifndef stands for if_not_defined
#ifdef stands for if_defined

One can use preprocessor directives to make more debugs through your local device.
One can define some specific header files only to be used in local device not on online_judge
One can use macro as flags inside these #ifndef or #ifdef containers

Examples:
(1)

#ifndef ONLINE_JUDGE
#define covid19 text
#endif

#ifdef covid19
cout<<"Yes, its corona";
#endif

(Here, we used covid19 as flag to print something.)
(2)
Thanks to Errichto as I learned from him on YouTube
https://github.com/Errichto/youtube/blob/master/atcoder-dp/g.cpp
(You can see on the above link he used #ifdef LOCAL for debugging, this will print stuffs local to the device)

For more information consider the following links
https://gcc.gnu.org/onlinedocs/cpp/Macros.html
https://en.wikipedia.org/wiki/C_preprocessor

Image source https://www.w3schools.in/cplusplus-tutorial/preprocessor/

Tags preprocessor, c++, online_judge, macros, #ifdef, #ifndef, local

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en12 English KR1shna 2020-09-04 18:19:10 56
en11 English KR1shna 2020-04-19 11:20:30 59
en10 English KR1shna 2020-04-17 20:02:33 2
en9 English KR1shna 2020-04-16 06:57:10 20
en8 English KR1shna 2020-03-30 15:27:32 5
en7 English KR1shna 2020-03-30 15:26:53 140
en6 English KR1shna 2020-03-30 13:51:13 1
en5 English KR1shna 2020-03-30 13:47:26 2
en4 English KR1shna 2020-03-30 13:46:36 66
en3 English KR1shna 2020-03-30 13:41:09 828
en2 English KR1shna 2020-03-30 10:34:55 16
en1 English KR1shna 2020-03-30 10:03:19 1392 Initial revision (published)