Hi,
I'm new to Codeforces and have been looking code into submissions by others. I'm new to C++ as well and can't find any articles on what these snippets of code do. Can someone please explain?
I understand that the code in If block gets executed when LOCAL is set. But what does the code actually do? What is alog/debug.g
? What is debug(...) 42
meant for?
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
Source: https://mirror.codeforces.com/contest/1712/submission/168158904
#ifdef LOCAL
#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
#define eprintf(...) 42
#endif
Source: https://mirror.codeforces.com/contest/1716/submission/166973182
.h
is called a header file, and is basically a library of code that either you make (or downloaded), or it comes with the compiler. Content-wise, it looks just like a C++ source file without amain()
A demonstration will be useful. Do the following:
algo
, in the same folder with your C++ file.sum.h
insidealgo
, then paste this code.You can do
include
in the header file, or add more stuff inside that header file, or add even more header files to the algo folder, or do any customizations you want to make your own algo library.debug.h
is probably just their own custom-made header file for debugging CP codes if needed. You might be able to find some online, or you can make one by yourself.Thanks for the answer!
Adding a few more details:
As MidoriFuse has explained algo/debug.h is a library used to help with debugging. Here is a sample debug.h file -> https://github.com/sanathkumarx/Competitive-Coding/blob/master/CodeForces/practice/algo/debug.h
#ifdef LOCAL
flag which is set locally by passing the command line arg-DLOCAL
when compiling the program.Refer: https://mirror.codeforces.com/blog/entry/78649
Whenever this flag is passed
debug(...)
function's definition will be#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
Otherwise
debug(...)
function's definition will get set to42;
which does nothing and gets ignored.Refer comments in: https://mirror.codeforces.com/blog/entry/99946
This is very useful, as you would not need to remove all your
debug()
statements when you are submitting code online.Why use 42 though? Is there any specific reason or just a randomly chosen number?
Stop trying to figure out how um_nik's mind works, you are doing it wrong, go and solve some problems.
Some jokes. IMO, as u accumulating more knowledge and implementing more and more code, at any unknown moments, u could unexpectedly understand some things that could seemed weird before. U would only find something brainy(helpful) when its meeting your current demands/situation.
More specifically, u may understand the use of the mentioned code when u are writing some “large” problems or u simply want to code faster.