C++ | hello world
First C++ program, hello world
The hello world program is one of the simplest programs, but it
already contains the fundamental components that every C++ program has.
Don’t be overwhelmed, we will take a look at the code line by line.
Type the code in your favorite editor (always type, don’t use cut/paste. This is better for learning purposes).
Save the program with the name: hello.cpp.
Program of Hello World
1: #include<iostream.h>
2: #include<conio.h>
3: void main()
4: {
5: //clear the screen.
6: clrscr();
7: cout<<"hello world ";
8: getch();
9: }
// A hello world program in C++
The first line in our program is a comment line. Every line that
starts with two slash signs ( // ) are considered comments and will have
no effect on the behavior or outcome of the program. (Words between /*
and */ will also be considered as comments (old style comments)). Use
comments in your programs to explain difficult sections, but don’t
overdo it. It is also common practice to start every program with a
brief description on what the program will do.
#include
Lines beginning with a pound sign (#) are used by the compilers
pre-processor. In this case the directive #include tells the
pre-processor to include the iostream standard file. This file iostream
includes the declarations of the basic standard input/output library in
C++. (See it as including extra lines of code that add functionality to
your program).
using namespace std;
All the elements of the standard C++ library are declared within what
is called a namespace. In this case the namespace with the name std. We
put this line in to declare that we will make use of the functionality
offered in the namespace std. This line of code is used very frequent in
C++ programs that use the standard library. You will see that we will
make use of it in most of the source code included in these tutorials.
int main()
int is what is called the return value (in this case of the type
integer. Integer is a whole number). What is used for will be explained
further down.
Every program must have a main() function. The main function is the
point where all C++ programs start their execution. The word main is
followed by a pair of round brackets. That is because it is a function
declaration (Functions will be explained in more detail in a later
tutorial). It is possible to enclose a list of parameters within the
round brackets.
{}
The two curly brackets (one in the beginning and one at the end) are
used to indicate the beginning and the end of the function main. (Also
called the body of a function). Everything contained within these curly
brackets is what the function does when it is called and executed. In
the coming tutorials you will see that many other statements make use of
curly brackets.
cout << “Hello World”;
This line is a C++ statement. A statement is a simple expression that
can produce an effect. In this case the statement will print something
to our screen.
cout represents the standard output stream in C++.
(There is also a standard input stream that will be explained in another tutorial). In this a sequence of characters (Hello World) will be send to the standard output stream (in most cases this will be your screen).
The words Hello World have to be between ” “, but the ” ” will not be printed on the screen. They indicate that the sentence begins and where it will end.
(There is also a standard input stream that will be explained in another tutorial). In this a sequence of characters (Hello World) will be send to the standard output stream (in most cases this will be your screen).
The words Hello World have to be between ” “, but the ” ” will not be printed on the screen. They indicate that the sentence begins and where it will end.
cout is declared in the iostream standard file
within the std namespace. This is the reason why we needed to include
that specific file. This is also the reason why we had to declare this
specific namespace.
As you can see the statement ends with a semicolon (;). The semicolon
is used to mark the end of the statement. The semicolon must be placed
behind all statements in C++ programs. So, remember this. One of the
common errors is to forget to include a semicolon after a statement.
Indentations
As you can see the cout and the return statement have been indented
or moved to the right side. This is done to make the code more readable.
In a program as Hello World, it seems a stupid thing to do. But as the
programs become more complex, you will see that it makes the code more
readable. (Also you will make fewer errors, like forgetting a curly
bracket on the end of a function).
So, always use indentations and comments to make the code more
readable. It will make your life (programming life at least) much easier
if the code becomes more complex.
Please comment on the post and share it.
And like it if you liked.
Please comment on the post and share it.
And like it if you liked.
0 comments :