Get started with your first program!
1. In this tutorial I’ll show you how to start your very first C++ program!
If you don’t have the appropriate tools, you can head over to Tutorial on getting started with C++ to get started right away!
2. You will also learn the structure of a C++ program.
Hello World
1. Open up your text editor, or IDE and type this in to an empty file/project.
#include
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
2. Run it using the appropriate commands found in my Tutorial on getting started with C++ depending on your environment and you should see Hello World in your terminal!
So what just happened?
I’ll explain what the previous code just meant.
#include
Includes the library iostream that contains the definitions for the input/output streams
using namespace std;
using namespace std allows us to use the entities that correspond to std. This is something you’ll usually have in all your programs.
int main()
{
This is the main function declaration where all programs start executing. in other words, whatever commands you have after this declaration will execute sequentially.
cout << "Hello World!\n";
This statement “cout” represents the output stream in C++ which outputs a sequences of characters to the stream which is usually the terminal.
“Hello World” is called a string.
\n indicates a newline.
return 0;
}
This return statement causes the main function to finish executing. The zero simply means that the program finished successfully. There are other codes, but I’ll explain those later.
Structure of a C++ Program
With the basic Hello World program you should follow the following guidelines when creating any program.
header files
using namespace std;
int main()
{
statements to execute your program
return 0;
}
Additional Resources
Getting Started for Programming shows you what you need in order to start programming.
Introduction to what C++ really is
Congratulations! You’ve made your first program! If you are having any trouble feel free to post a comment and I’ll respond promptly!
