[C++ Tutorial 3] Basic Program Structure


So here’s our very first program in C++. We’re gonna take a look at it and then see what each line of code means.

<span style="color: #000099;">#include&lt;iostream&gt;
</span><span style="color: #990000;">using namespace</span> std<strong><span style="color: #663300;">;</span></strong><span style="color: #ff6633;">

int</span><span style="color: #990000;"> main</span><strong><span style="color: #663300;">(
cout<strong><span style="color: #663300;">&lt;&lt;</span></strong><span style="color: #009900;">"Hello Jolly!"</span><strong><span style="color: #663300;">;</span></strong>
system<strong><span style="color: #663300;">(</span></strong><span style="color: #009900;">"pause"</span><strong><span style="color: #663300;">);</span></strong><span style="color: #ff0000;">
return</span><span style="color: #999900;"> 0</span><strong><span style="color: #663300;">;


Here, we’re using the code already there in the file iostream in own program. So the compiler is told to get the code from iostream and compile it before moving on to compile the rest of the code. The “<“ and “>” are used when the file is located in the default compiler bin and not in an external location. You can try making your own iostream file. Copy all code from iostream to any file, say phoenix.txt and use #include<phoenix.txt> and it’ll work.

using namespace std;

Before anything else, you need to know that all statements end with a semi-colon. So, this is your first C++ statement. This just means that you’re using a namespace called the standard library. Now, this is done to avoid name collisions. For instance, consider a university which has a student with roll number 70. Another university may also have a student with the same roll number. Now, these are two namespaces, and by specifying  which namespace we’re using, we can differentiate between the two students.

int main(){…}

All he shit that we do before this statement is just getting things ready and set for the main function. Did i say function? Yes, this is your first C++ function. Like any function, it must return some data. We use “int” here to show that the returned data would be an integer. Every function has pretty much the same format.

It goes like this-> return-type func-name (parameters){statements}

cout<<“Hello Jolly!”;

Now this is another C++ statement. If you have seen the output of the program using your compiler-which is what you should have done. You’ll only see Hello Jolly! printed on a stupid blank black screen. Well, there you go-the cout is your standard output function. Anything in the double inverts with the output operator “<<” goes as output to the screen. This function, along with the standard input function(cin) is stored in the “std” file which we talked about in the namespace statement.


A program like this one would usually run in a matter of nanoseconds and the window would close in a flash, unless mi friend-you use the above to simply, pause the system-causing it to wait until you exit.

return 0;

Did I say the main function in C++ would return an integer? So, here we return 0-nada-nothing-goose egg.

Again, the most important thing is to go and try it out for yourself.

[Go to Tutorial 4]

Get the best of Durofy in your inbox


About Author

Founder at Durofy. Blogs on technology & startups, runs a full service digital agency, teaches AP Calculus.


  1. Pingback: Durofy » C++ Tutorial 2 – Multi-Paradigm Programming