Hey,

In my cpp template, I use to have ONLINE_JUDGE macro like others for input,output in local development. But, today I discovered there is a better way of using it like having a macro Local. So that the behaviour will be local to my setup then to expect any site to support the ONLINE_JUDGE macro.

Few tips along the way too

The syntax for macros is

# define Area(l,b)(l*b)

Macros is preproccessed before compiler compiles the source code. Macros helps in reducing the redundancy. It’s also useful if the program needs to execute differently in different environment i.e output and execution depending on the environment

Example:

  • As we use ONLINE_JUDGE to differentiate the sites and local.
  • Depending on the OS
  • To reduce the redudancy like area(l,b); (But I think l*b is better than Area(l,b) for CP)
  • But MIN(x,y)(x<y?x:y) is better than using min(x,y). As in the latter, we need to have x and y types as int.

Macros to have in CP template:

#define MIN(x,y) (x<y?x:y)
#define MAX(x,y) (x>y?x:y)
#define DEBUG(str) (cout<<"\n"<<str<<"\n"<<endl)

int main(){
#ifdef LOCAL
    freopen("in","r",stdin);
    freopen("out","w",stdout);
#endif

    //code

}

Macros for development:

void ff(){

#ifdef DEBUGMODE
    cerr<<"Entered into ff"<<endl;
#endif

    //LOGIC

#ifdef DEBUGMODE
    cerr<<"Exiting from ff"<<endl;
#endif
}

Finally I have added the above lines in my CP template.