This is a C++ introduction tutorial for beginners with an example that will print the text “Hello, World!” written in C++ into the console window.
Hello World! is one of the most ubiquitous examples in any programming language. All it does is to print Hello World!
and exit the application.
As with many things, C++ can do one thing in many ways.
Hello World!
This simple program will print Hello world! in C++ followed by a newline.
Here’s how you create a new C++ project in Visual Studio to run this program.
Start with creating a new CPP file: main.cpp:
<span class="ot">#include <iostream></span>
<span class=“dt”>int</span> main()
{
<span class=“co”>// C++-style comment in a C++ program</span>
std::cout << <span class=“st”>“Hello world!\n”</span>;
}
What is happening?
Line by line explanation.
<span class="ot">#include <iostream></span>
Lines starting with a hash (#
) are for the preprocessor. The preprocessor looks at all lines starting with a hash, and does something based on the command. The command is #include
and it replaces #include <iostream>
with the contents of a file called iostream
. iostream
is a file coming as a part of your compiler.
The file <iostream>
gives us some things, namely the std::cout
stream. We’ll get back to that.
<span class="dt">int</span> main()
{
}
int main()
is a C++ main function and is a special method where the program starts to run. main()
comes in many forms and shapes, but is almost always called main
.
<span class="co">// C++-style comment in a C++ program</span>
This is a comment. Comments are completely ignored by the compiler. They are there from a developer to the next developer, and for your future self. Comments can be used to document an algorithm, what the programmer is thinking and general advice to the next programmer working on the code.
Comments are also immensely useful because it’s much harder to read code, than it is to write code. Good comments will mitigate that.
std::cout << <span class="st">"Hello world!\n"</span>;
Finally the line that prints out Hello World! to the console, followed by a newline (n
).
printf
There is another way to print out “Hello, World” – by using the printf
-method.
Before getting started, it’s prudent to state the following: printf is not C++.
Many aged forums posts and Hello World! C++ examples will contain a variation of printf
-based Hello World! examples. That is not C++, it’s C. Not that C is bad, but C does things one way, while C++ does it differently.
This is not C++. However, this is perfectly valid C. Even though it is valid C, it’s also valid C++. It doesn’t mean it’s a good idea to use it in a C++ program though.
<span class="ot">#include <stdio.h></span>
<span class=“dt”>int</span> main()
{
<span class=“co”>/* C-style comment in a C program */</span>
printf(<span class=“st”>“This is not C++, it's C\n”</span>);
}
As much as possible, we’ll try to avoid C constructs where there are equivalent C++ constructs.
Try the next tutorial: Hello, World! with classes.
See Also
- - C++ Hello World with Classes
- - C++ Smart Pointers: weak_ptr
- - Default explicit equality operator
- - Mistakes
- - compile_commands.json with CMake