Computers

Hello World of C++

Posted on by in C / C++

People, I know you do not want yet another tutorial explaining the theoretical aspects of C++. So let us get to work. Let’s write the first C++ program and understand the basics…;)

// my first program in C++

#include <iostream>

using namespace std;

int main ()

{

cout << “Hello World!”;

return 0;

}

Output:

Hello World!

The above program is one of the simplest programs that can be written in C++. Nevertheless, it contains the basics that we need to understand at first. Follow the below notes and you will know the basics of C++!

Note1:

// my first program in C++ – This is a comment line. “//” can be used to comment any line in the program. However, double slash (//) can be used to comment only one line. If you need to comment a block of code below is the way to do it.

/* Comment 1

Comment 2

Comment 3 */

Note 2:

#include <iostream> – In C++, the lines beginning with a # are preprocessor directives. Simple this means these are instructions for the preprocessor to extract library files. Then the preprocessor knows where to look for! iostream is the standard input output library in C++. In the program, we have used cout, which is an output function. So in order to use cout we need iostream. It’s as simple as that!

Note 3:

using namespace std; – All the elements of standard C++ library are declared within std namespace. So in order to access these files we need the namespace std.

Note 4:

int main () { } – This is the main function of C++ programs. Regardless where it is located in the code the execution of the program begins here. Consequently, it is essential that all C++ programs have a main function. (Now guys that’s something to remember. Otherwise you might wonder why your programs don’t run.)

Like any other function in C++ main should be followed by a pair of parentheses (()). Later we will write programs, which include parameters within these parentheses. For now, we are just concerned about the basics. You can see a pair of curly braces right after these parentheses ({}). Now that’s your place to write the function declaration. Simply, what is within these braces is what the function does!

Note 5:

cout << “Hello World!”; – This is an output statement. Simply, what this line does is output the sentence “Hello world!”. To use this cout only we went through the trouble of giving preprocessor directives. You can see that this line ends with a semicolon. In fact all the statements in C++ should end with a semicolon and forgetting to type this semicolon (;) is one of the most frequent errors. (Well not all statements. As you can see, # include and function signature seen in this program are exceptions.)

Note 6:

return 0; – This statements terminates the program. main function requires an integer to be returned. By returning this zero, the program merely indicates to have run without any error. This is how most of the C++ programs terminate.

Of course if you use void main there is no need of this line. (However, use of int main is much encouraged!)

You just learnt the basics of C++!!!

Share this post

4 thoughts on “Hello World of C++”

  1. Hanxlk says:

    Nice post on C programming 101!

  2. malhora says:

    love the way of having more POSIX friendly approach 😉

  3. Ruzeen says:

    Interesting stuff ya

  4. William Gogarty says:

    Wondering if you have any Visual Basic programming you could put on this site

Comments are closed.