Блог пользователя KR1shna

Автор KR1shna, история, 5 лет назад, По-английски

My first blog. Seeking some positive feedback
It might be useful to some of us
Introduction:
Before a C++ program gets compiled by the compiler, the source-code gets processed by the compiler. This technique is called preprocessor.
This technique is not a part of the compiler but it is a separate method that comes under compilation process. It directs the compiler that the information should be preprocessed before the actual compilation starts.
All preprocessor directives in C++ begin with #, and they do not need to end with a semicolon(;) because this is not a statement in C++.
Imagw
Look at the following piece of code in C++:

 
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define macro1
#define macro2
...
#include "local_header1.h"
#include "local_header2.h"
...
#endif
//.....//
#ifndef stands for if_not_defined

#ifdef stands for if_defined

One can use preprocessor directives to make more debugs through your local device.
One can define some specific header files only to be used in local device not on online_judge
One can use macro as flags inside these #ifndef or #ifdef containers

Examples:
(1)

 
#ifndef ONLINE_JUDGE
#define covid19 text
#endif

#ifdef covid19
cout<<"Yes, its corona";
#endif
(Here, we used covid19 as flag to print something.)
(2)
Thanks to Errichto as I learned from him on YouTube
https://github.com/Errichto/youtube/blob/master/atcoder-dp/g.cpp
(You can see on the above link he used #ifdef LOCAL for debugging, this will print stuffs local to the device)

For more information consider the following links
https://gcc.gnu.org/onlinedocs/cpp/Macros.html
https://en.wikipedia.org/wiki/C_preprocessor

Image source https://www.w3schools.in/cplusplus-tutorial/preprocessor/

  • Проголосовать: нравится
  • +35
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Is there any macro that can be simulatneously be used both for local testing and while submitiing on CSES?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

    Errichto in his educational dp atcoder video talked about this, and shared this command in terminal, he mentions that he use this for local testing/debugging and when submitted to any online judge it will not be evaluated; You can refer this video:https://www.youtube.com/watch?v=FAQxdm0bTaw

    Command Shared in chat of that video: g++ -DLOCAL -std=c++17 -Wshadow -Wall -o "h" "h.cpp" -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -g (in directory: /home/kamil/youtube/atcoder-dp)

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится -14 Проголосовать: не нравится

      So there is no way in which we can use the same code template on our local machine as well as on CSES?

      • »
        »
        »
        »
        4 года назад, # ^ |
        Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

        I don't know what CSES is (Comparative Study of Electoral Systems?..), but why would you need any macros for that? The whole point of these macro tricks is to disable some code when running in a judge, so if you want to use the same code template for both your local machine and the testing system, just submit it as-is.

        Edit: I think I figured out what you were saying. If you meant cses.fi, there are apparently no custom defines, so you'll have to use the traditional #ifdef LOCAL thing as mentioned above (see submissions by tourist for practical examples).

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          But when I use #ifdef LOCAL I'm not able to test my code locally as

          #ifndef ONLINE_JUDGE
          	// for getting input from input.txt
          	freopen("input1.txt", "r", stdin);
          	// for writing output to output.txt
          	freopen("output1.txt", "w", stdout);
          #endif
          

          as these lines are needed for my code to run locally

          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Don't use ONLINE_JUDGE if you want to run your code on that testing system, it's not defined there..

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Why would you need a macro for this? Just write that code without wrapping it in an ifdef.