Ch2.1: Hello World
Your First Program (Minimal)
Now that you know how to compile code, let’s write your very first C++ program. This program will simply print Hello World on the screen.
#include <fast_io.h>
int main()
{
using namespace ::fast_io::iomnp;
print("Hello World\n");
}
Your First Program (With Comments)
Here’s the same program again, but with comments explaining each part:
// Single-line comment: starts with //
// Multi-line comment:
/*
This is a multi-line comment.
It starts with /* and ends with */.
*/
// Documentation-style comments:
/// One-line documentation comment
/**
* Multi-line documentation comment
*/
#include <fast_io.h> // include the fast_io library
// Every C++ program starts with a main function.
int main()
{
// "using namespace ::fast_io::iomnp;" lets us use fast_io functions directly.
using namespace ::fast_io::iomnp;
// "print" writes text to the screen.
print("Hello World\n");
}
Breaking It Down
- Comments:
//single-line comments/* ... */multi-line comments///documentation comments/** ... */block documentation comments
- #include <fast_io.h>: Brings in the
fast_iolibrary so we can use its functions. - int main(): The entry point of every C++ program. The computer starts running your code here.
- Curly braces { }: Group the statements that belong to
main. - Semicolon ;: Marks the end of a statement, like a period in a sentence.
- print("Hello World\n");: Prints text to the screen.
\nmeans “new line.”
Try It Yourself
Save the minimal code above as main.cpp.
After compilation and then run it you should see:
Hello World