Hello World in C++!

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!

Tags:

  • Diego

    I still remember when I started this program way back! It’s pretty straightforward! If anyone has any questions, feel free to post them here!

  • Simon Liu

    Great article!

  • Simon Liu

    Great article!

  • http://www.trickylinux.com Steven Russo

    Hey awesome blog! Know anyone who would be interested in part time admin’ing a Linux box? Hit me up!

  • http://www.trickylinux.com Steven Russo

    Hey awesome blog! Know anyone who would be interested in part time admin’ing a Linux box? Hit me up!

  • Gerard Mopecha

    Hi, I am programming knowledge in Java but wish to expand my programming knowledge into C/C++. After reading your post on this site, I was so exited to be able to start programming in C++. Everything went well except that the console window that prints the out was not displayed when I ran the application. Instead, I had the following message below the box where I typed my code
    (
    “Hello – Debug” uses an invalid compiler. Skipping…
    Nothing to be done.
    )
    and the “Build Lod” on the taskbar above it was highlighted. I don’t know if the problem is mind or not. I would be very grateful if you respond to my posting.
    Thanks in advance for your help.

  • Gerard Mopecha

    Hi, I am programming knowledge in Java but wish to expand my programming knowledge into C/C++. After reading your post on this site, I was so exited to be able to start programming in C++. Everything went well except that the console window that prints the out was not displayed when I ran the application. Instead, I had the following message below the box where I typed my code
    (
    “Hello – Debug” uses an invalid compiler. Skipping…
    Nothing to be done.
    )
    and the “Build Lod” on the taskbar above it was highlighted. I don’t know if the problem is mind or not. I would be very grateful if you respond to my posting.
    Thanks in advance for your help.

  • Diego

    Can you provide more information?

    What operating system are you using? Linux, Windows, Mac OSX?

    Have you installed gcc yet in order to compile using g++?

    The above code should work fine. The file should be called something like main.c and compiled with ‘g++ main.c’.

    Getting started for programming might help.

    Tell me if your problem still persists!

  • Gerard Mopecha

    Hi, Diego!

    Thanks alot for your help. My computer was bad, so I could not get in touch with the forum members. Actually, I read some tutorials I had save and had the problem sorted out. I’m a little comfortable program both in C and C++ now. I’m using Windows Vista.

    Again, thanks for your reply.

  • Gerard Mopecha

    Hi, Diego!

    Thanks alot for your help. My computer was bad, so I could not get in touch with the forum members. Actually, I read some tutorials I had save and had the problem sorted out. I’m a little comfortable program both in C and C++ now. I’m using Windows Vista.

    Again, thanks for your reply.