C++ Hello World
By
Kent, last updated November 1, 2019
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:
#include <iostream>
int main()
{
// C++-style comment in a C++ program
std::cout << "Hello world!\n";
}
What is happening?
Line by line explanation.
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.
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
.
// C++-style comment in a C++ program
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 << "Hello world!\n";
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.
#include <stdio.h>
int main()
{
/* C-style comment in a C program */
printf("This is not C++, it's C\n");
}
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.
C++ Hello World
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:
What is happening?
Line by line explanation.
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 callediostream
.iostream
is a file coming as a part of your compiler.The file
<iostream>
gives us some things, namely thestd::cout
stream. We’ll get back to that.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 calledmain
.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.
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.
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.
Related posts:
How to create new C++ project in Visual Studio 2017
C++ Hello World with Classes
C++ Function Templates with Examples
C++ Variables and Data Types
Variable Scopes in C++
Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.