Before reading this article I recommend you to read the following...
Program to print a sentence on the screen
//My first C++ program
#include<iostream.h>
int main()
{
cout<<"WELCOME TO C++ PROGRAMMING";
return 0;
}
output;
The above program is one of the simplest programs that can be written in C++, but it does include the basic elements that every C++ programs has. Let us have a look at these elements one by one:
If you know about the c++ and its basics then you can proceed.
Program to print a sentence on the screen
//My first C++ program
#include<iostream.h>
int main()
{
cout<<"WELCOME TO C++ PROGRAMMING";
return 0;
}
output;
output... |
The above program is one of the simplest programs that can be written in C++, but it does include the basic elements that every C++ programs has. Let us have a look at these elements one by one:
//My first C++ program
This is a comment. All lines beginning with // are comments. Compiler does not execute comments. Comments are included for the programmers for short explanations or observations.
#include<iostream.h>
Statements that begin with # (has/pound) sign - are directives for the preprocessor. That means, these statements are processed before compilation takes place. The #include<iostream.h> statement tells the compiler's preprocessor to include the header file iostream (notice the name iostream.h)in the program.
int main()
This line indicates the beginning of main function. The main() function is the point by where all C++ programs begins their execution. In-fact, the content of main() is always the first to be executed when a program starts. And for the same reason, it is essential that all C++ programs have a main() function.
Every function in C++ has its code included in a pair of curly braces({}) so that code below line int main() inside { and } is code of function main().
cout<<"WELCOME TO C++ PROGRAMMING";
This instruction does the most important thing in this program. The cout is the standard output stream in C++ (usually the screen of monitor ) and the above statement inserts a sequence of characters - "WELCOME TO C++ PROGRAMMING" here -- into this output stream (i.e., the screen of monitor). Since cout is declared in header file iostream.h, so in order to use cout in the program, we have included this very header file with #include directives.
Notice that this statement ends with a semicolon character (;). The semicolon (;) character is used to finish every executable statement of a C++ program and it must be included after ever executable instruction (#instructions are not executable instructions).
return 0;
The return instruction makes the main() to finish and it returns a value, in this case it is returning 0 (zero). Returning 0 is the most usual way of telling that program has terminated normally i.e., it has not found any errors during its execution .
After discussing the most basic elements of C++ program, let us take our discussion further by taking up some other pages.
0 Comments