Roufie's blog

By Roufie, history, 3 years ago, In English

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://codeforces.net/contest/1712/submission/168158904

#ifdef LOCAL
	#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
	#define eprintf(...) 42
#endif

Source: https://codeforces.net/contest/1716/submission/166973182

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

| Write comment?
»
3 years ago, # |
Rev. 2   Vote: I like it +42 Vote: I do not like it

.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 a main()

A demonstration will be useful. Do the following:

  • Make a C++ file like how you usually do on your computer. Paste this code in.
something.cpp
  • Make a folder named algo, in the same folder with your C++ file.
  • Make a file named sum.h inside algo, then paste this code.
sum.h
  • Now run the C++ code (the "something.cpp" file)

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.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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://codeforces.net/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 to 42; which does nothing and gets ignored.

    Refer comments in: https://codeforces.net/blog/entry/99946

    This is very useful, as you would not need to remove all your debug() statements when you are submitting code online.

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

      Why use 42 though? Is there any specific reason or just a randomly chosen number?

      • »
        »
        »
        »
        4 weeks ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        It comes from a novel by Douglas Adams called 'Hitchhiker's guide to the galaxy'. This number is known as an "answer to the Ultimate Question of Life, the Universe, and Everything".

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.