In this
tutorial, we are going to code our first program: Hello World Program. This program is considered as the most common, even learning of any
programming start with this program. In this program we are going to simply print “Hello World” on Console (Black Command Prompt Screen).
So let’s
move ahead…
This is how
a C++ Hello World Program looks
like:
Now we will
discuss all the statements one by one.
Consider,
As discussed
in the previous article, firstly we include
Header File which we will need in our program. “#include” is a Preprocessor Directive to include header files in
our program. Preprocessor Directives are the line of code that starts with “#”, they are called so
because they are executed by
Preprocessor before the compilation of code (it’s obvious that we will need
to include files before the process of compilation). “#include” is used to include files and “#define” is used to define Macros.
There are some more Preprocessor Directives, but this two are most commonly
used.
The name of
the Header File to be included is provided within Angular Braces (like #include <iostream>) or within Double Quotes (like #include
“iostream”).
The iostream header file provides us with stuff
for Basic Input and Output. Iostream stands for the input-output
stream.
Consider,
Line No. 6,
it defines our main function which is the entry
point of our program.
The “int” is a data type which specifies
what our main function is going to return at the end. It is called as the return type of function (here main
function). We will learn more about them in the Function’s article.
“main” is the name of the function.
“main()”, this parenthesis specifies that main
is a function.
Every
function starts with an Opening Curly
Brace “{“ and ends with a Closing
Curly Brace “}”.
Consider,
Here we do
our task of printing Hello World.
“std::cout” is the object of class ostream which is used to print on screen in C++, it is defined
in “iostream” Header File. Since it belongs to the standard class of C++,
we need to add “std::” in front of it.
“<<” is called as Insertion Operator, which insert
the string “Hello World” inside cout object. The cout object is further
connected to monitor via streams. It passes the string to monitor to print it.
“;” the semicolon
ends all the statements in C++. It is used to indicate the end of a valid
executable statement.
Consider,
Line No. 11:
“return 0” statement returns 0 from
main(). Operating System calls main() of each program, and in C++, main() must return an integer to Operating
System and our return statement does
the same.
The semicolon
at end completes the return statement.
Consider,
“}” brace
completes our main() and completes our Program too.
So now, let's Compile and Run our program to see results. The Output of Program Comes like:
So…Bingo, we have done with our
Hello-World Program.
______________________________________________________
if any doubt or feedback please feel free to contact :
rahulrb15899@gmail.com
+91 7775919753 (WhatsApp)
No comments:
Post a Comment