i have seen in different codes this instruction
#ifdef ......
...
#endif
and my question is what does it mean?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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?