Chapter 2: Environment Setup

📅
⏱️3 min read
English

Covers the fast, stable, and accurate toolchain you need: compilers, IDEs, I/O tweaks, and file redirection.

In competitive programming, our minimal requirements for a workable setup are fast, stable, and precise.

  • Fast: quick compilation, instant execution, and rapid test input.
  • Stable: no crashes, full support for at least C++11 (better C++14/17/20).
  • Precise: an environment that mirrors contest judges as closely as possible.

2.1 Compiler: GCC / G++

No matter which editor you prefer, the compiler is where the real work happens. The CP community (OI, ICPC) standardizes on GCC (GNU Compiler Collection).

  • Windows users: install MinGW-w64.
    • Simple path: download an IDE such as Dev-C++ or Code::Blocks that bundles the compiler and avoids manual PATH tweaks.
    • Pro path: install MinGW separately and add g++ to your system PATH so you can run it from the terminal.
  • macOS users: open Terminal and run g++ --version. If it is missing, macOS will prompt you to install the Xcode Command Line Tools—just confirm.
  • Linux users: you are already set. Most distros ship with GCC.

Verify installation: run g++ --version. If you see a version number (e.g., 11.2.0), the compiler is ready.


2.2 IDE Recommendations: Pick a Weapon

There are dozens of tools, but for contests, pick the scenario that fits you:

  • Who: most people, especially those stacking both contest and engineering work.
  • Why: rich extensions, polished interface, lightweight.
  • Must-have extensions:
    1. C/C++ (official Microsoft extension)
    2. C/C++ Compile Run (one key to build and run)
    3. 🔥 Competitive Programming Helper (cph)—a magical plugin that grabs test cases from judges and runs them locally. Strongly recommend installing it.

Option B: Dev-C++ (Retro + Exam Ready)

  • Who: middle school students prepping for CSP-J/S or NOIP.
  • Why: compliant with many exam rooms where outdated machines only ship with this IDE. It lacks autocompletion, but it is simple and consistent.

Option C: CP Editor (Built for Contests)

  • Who: purists who only do CP and don’t want to fiddle with config files.
  • Why: left-side code, right-side I/O, one-click submissions to Codeforces. Plug-and-play.

2.3 The Magical Header #include <bits/stdc++.h>

In standard software projects, you include each header (<iostream>, <vector>, <algorithm>). In contests, we cheat with one line:

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    // You can now use nearly every standard library symbol—sort, vector, max, min, etc.
    return 0;
}
  • Pros: saves time and keystrokes.
  • Cons: slightly longer compile time and it is non-standard, but GCC (the default on judges) supports it.
  • Verdict: feel free to use it during contests.

2.4 Speed up I/O: Say Goodbye to TLE

cin/cout are convenient, but they sync with C I/O by default, which slows them down. If your data size hits 10^5 or 10^6, this default can cost you the contest.

Add these two lines at the top of your main:

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    // Your logic...
    return 0;
}

Warning: once you disable synchronization, do not mix cin/cout with scanf/printf. The output order will be unpredictable.


2.5 File Redirection: Escape the Black Console

Typing long test cases into the console is error-prone. Let your program read from files:

#include <bits/stdc++.h>
using namespace std;
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout); // optional output redirection
#endif
 
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int a, b;
    while (cin >> a >> b) {
        cout << a + b << endl;
    }
    return 0;
}

Place sample input in in.txt, run your code locally, and it will automatically read from the file. On the online judge, the ONLINE_JUDGE macro is defined, so the redirection disappears.


🛠️ Homework

  1. Install VS Code (or Dev-C++).
  2. Add the C/C++ extension.
  3. Write and run a “Hello World” that includes ios::sync_with_stdio(0); and #include <bits/stdc++.h>.

Once your environment is ready, Chapter 3 will arm you with the STL essentials.

Share this article

Related Posts

Coming soon...

Comments

Coming soon...