lusho's blog

By lusho, history, 7 years ago, In English

i have seen in different codes this instruction

#ifdef ......

...

#endif

and my question is what does it mean?

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it +15 Vote: I do not like it

Why don't you just google it?

»
7 years ago, # |
  Vote: I like it +37 Vote: I do not like it

It's one of C/C++ compiler preprocessor, it tells the compiler to choose what line of code to be compiled if it meet some condition.

Let me explain using examples:

filename: x.cpp

If you compile the code above with g++ -o x x.cpp -D_use_cin_cout the cin/cout code will be executed, if you compile it normally without -D_use_cin_cout option it will use scanf/printf.

It also good for debuging you don't need to comment out the debug code when you ready to submit, just use ifdef something and compile it with Dsomething

Other useful example is in USACO contest the submitted code is expected to use file as I/O while when I test my code locally I use console stdin/stdout. To settle this (without changing any code when submitting) i use something like this:

usaco code

so when I test my code, I will compile it with g++ -o x x.cpp -Dmy_name the compiled code will use console stdin/stdout (ifndef means if not defined), and I don't need to change my code when I want to submit this to USACO because of course USACO didn't compile it with -Dmy_name or have #define my_name in their system :) And don't forget to change "problem_name" when switching to other problem.

Other useful example is to deal with scanf/printf problem with %lld and %I64d it's system dependent but because gcc will define "__unix__" if it run under linux, I can use this piece of code:

I/O code

Of course I won't use that trick because it'll be much easier to use cin/cout, but I just showing you what power #ifdef and something similar can do.

For more detail (there are a lot more examples) you can search it on the internet yourself.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What is the benefit of #ifdef , ifndef than comment out?

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it
      #ifndef ONLINE_JUDGE
      
      #define mylog(...)   printf( __VA_ARGS__ )
      #else 
      #define mylog(...)   /****nothing****/
      #endif
      
      int main()
      {
      // judge compiler this 4 lines comment out, but your compiler will use input.txt 
      //  and output.txt for input | output instead of standard console. 
      #ifndef ONLINE_JUDGE
           freopen("input.txt", "r", stdin);
           freopen("output.txt", "w", stdout);
      #endif 
      
           int n; scanf("%d",&n);
           mylog("n = %d",n); // it comment out automatic when compiled on judge compiler
      
           int a[100] = {};
           for(int i= 0; i< n;++i)scanf("%d",a+i);
      
           int s = 0;
           for(int i = 0; i < n; ++i){
               s ^= a[i] ; // some operation
      
          // it comment out on judge compiler, but on your on compiler prints i and s.
               mylog("s at position %d equal to %d\n", i, s);
           }
           return 0;
      }