i have seen in different codes this instruction
#ifdef ......
...
#endif
and my question is what does it mean?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Name |
---|
Why don't you just google 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:
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 withDsomething
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:
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: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.
What is the benefit of #ifdef , ifndef than comment out?