0

Most of the people write this in their code.

What dose it mean?

what is the use of #ifndef.

#ifndef ONLINE_JUDGE
   freopen("E://ADpan//in.in", "r", stdin);
   freopen("E://ADpan//out.out", "w", stdout);
#endif
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Haddi
  • 71
  • 1
  • 4
  • 11
    Contrary to "Most of the people", this is the first time I've seen this macro. Related: [Why are #ifndef and #define used in C++ header files?](https://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files) – TrebledJ Jun 07 '19 at 07:22
  • 1
    *"Most of the people write this in their code."* Nope, I've never seen this before. *"What dose it mean??"* my guess it it's for conditional compilation for when an online judge compiles it, perhaps because they have different IO settings than your IDE. *"what is the use of #ifndef."* look up "C++ preprocessor" – Blaze Jun 07 '19 at 07:23
  • 5
    A cursory google search led to [this post](https://codeforces.com/blog/entry/14118). To summarize: there are online coding competitions where predefined input is sent to stdin and expected on stdout. When testing and developing locally, some people prefer to hack their program to read from files instead, so they do not have to do something like `test_program < input | diff - expected_output`. Finally, the `ONLINE_JUDGE` macro is defined in the competition environment, so that uses stdin/stdout, while a local compilation will not have that macro and thus read from those files. – Botje Jun 07 '19 at 07:26
  • @Botje, converted your comment into a community wiki answer. – Evg Jun 07 '19 at 08:17

4 Answers4

5

A cursory google search led to this post. To summarize: there are online coding competitions where predefined input is sent to stdin and expected on stdout. When testing and developing locally, some people prefer to hack their program to read from files instead, so they do not have to do something like

test_program < input | diff - expected_output

Finally, the ONLINE_JUDGE macro is defined in the competition environment, so that uses stdin/stdout, while a local compilation will not have that macro and thus read from those files.

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 1
    It could also be used for other things, such as when your program uses non-portable stuff and your compiler/environment is different from the judging servers, measuring execution time, debug code, automatic testing code, etc. – eesiraed Jun 07 '19 at 17:17
1

This line of code is used by competitive programmers who code in a text editor instead of an an IDE. By default the ONLINE_JUDGE constant is defined when submitting code in most online judges ex: codeforces or codechef. It helps the code to determine whether the code is being run on an online judge or on a local system machine. This line of code is used for the code to read & write from a file rather than stdin when running on a local machine as well as reading & writing from stdin and stdout, respectively when run in an online judge.

0

As a competitive programmer, this is something I use a lot. This means that during testing and implementation, you can do specific things - in this case, you are reading stdin and stdout from files to make debugging easier. When the code is compiled online and submitted, the ONLINE_JUDGE flag is set so that this doesn't run and they can use specific files to validate your program.

George Ogden
  • 596
  • 9
  • 15
0

Regarding the first part of your question about freopen(): In competitive programming, many contests request input and output from standard input/output. However, some problems (for example, many of the USACO problems) require you to read and write from a file. The freopen() command is defined in the <stdio.h> header file and allows you to redirect the input/output streams to files. You can choose to read or write to a file, along with some other ways to manipulate the file like append. The documentation can be found here.

In order to answer the second part of your problem "what is #ifndef", you need to know a few things:

  • The C++ preprocessor performs preliminary operations on the code before passing it to the compiler. For example, you can define certain shortcuts that make the programming process easier.
  • The #include "filename" directive is replaced by the textual contents of the file. Files can include each other, for example, "Person1.h" can include "Person2.h" and vice versa.
  • In C++, classes, and functions can only be defined once (except for overloads) in the whole program.

Now, to answer your question: #ifndef is a preprocessor directive which stands for "if not defined" that is always paired with #define and #endif and usually acts as an include guard. This prevents situations where files include each other infinitely, or each define the same class/function twice, both of which will cause errors. For example:

//file "person1.h"
#include "person2.h"
#ifndef PERSON1_H
#define PERSON1_H
class Person {
    int age;
};
#endif
//file "person2.h"
#include "person1.h"
#ifndef PERSON2_H
#define PERSON2_H
class Person {
    int age;
};
#endif

If you didn't put #ifndef in the files above, then person1.h would replace the first line with the textual contents of person2.h, but notice that person2.h also has an #include person1.h which would create an infinite loop. In addition, you would also have the Person class defined twice, which is illegal.