Skip to content

Hello, World!

Environment Setup

A craftsman must sharpen his tools to do his work well.

Integrated Development Environment

IDEs are relatively simple to operate, and beginners often choose IDEs to write code. In competitions, Dev-C++ is the most common (if the exam environment is Windows, this IDE is usually provided).

Compiler

Windows

It is recommended to use the GNU compiler. You need to download and install MinGW from MinGW Distro. Alternatively, on Windows, you can also choose the Microsoft Visual C++ compiler, which requires downloading and installing from the Visual Studio page.

macOS

Execute in the terminal:

1
xcode-select --install

Linux

Use g++ -v to check if g++ is installed.

You can install it with the following command:

1
sudo apt update && sudo apt install g++

Compiling Code in the Command Line

Once proficient, some players prefer using the more flexible command line to compile code, eliminating dependency on an IDE and allowing them to write code with their favorite text editor.

1
g++ test.cpp -o test -lm

g++ is the compiler for the C++ language (the C language compiler is gcc), -o is used to specify the filename of the executable, and the compilation option -lm is used to link the math library libm, enabling code that uses math.h to compile and run normally.

Note: C++ programs do not require -lm to compile and run normally. However, the C++ compilation options for past NOI/NOIP problems have included -lm, so it is added here as well.

First Code

Let's begin the C++ introductory journey with this example program~

Note: Please ensure English input mode is enabled before writing.

C++ Language

1
2
3
4
5
6
#include <iostream>  // Include header file

int main() {                     // Define main function
  std::cout << "Hello, world!";  // Use cout function from the standard namespace
  return 0;  // Return 0, ending the main function. Compilers usually add this line automatically, and it can generally be omitted
}

C Language

1
2
3
4
5
6
#include <stdio.h>  // Include header file

int main() {                // Define main function
  printf("Hello, world!");  // Output Hello, world!
  return 0;                 // Return 0, ending the main function
}

Note: The C language is provided here for reference only. C++ is largely compatible with C and offers many new features that can help contestants work more efficiently during competitions. For details, see Differences Between C++ and Other Common Languages.