Showing posts with label iostream. Show all posts
Showing posts with label iostream. Show all posts


C++ Program Structure -I

                                           Structure of a C++ Program
A program is a sequence of logical instructions that can be executed by a compiler in a computer. All programs are written to solve any problem by programming language. The best way to learn a programming starts from the root of that language. Therefore, here we will try to write our first program:


// This is my first program in C++

# include <iostream>
using namespace std ;

int main ()
      cout << " Hello C++ Programming \n" ;
      return 0;



Hello C++ Programming

The first step shows the source code for our first program. The second step shows the result of the program once compiled and also executed. The source code writing style and way of compiling a source code (program) depends on the version of compiler and development environment installed in your computer. The previous program is the typical program that programmer trainee writes for the first time in his/her education life. Apparently the result on screen is “Hello C++ Programming”.  

The substantial things is it is contains the fundamental components that every C++ program has. Maximum person tries this easy program by reading a blog or book. We are going to look line by line at the code we have just written:

// my first program in C++

This line is used for showing comments. Any lines beginning with two slash signs (//) are considered comments. Comments lines do not have any effect on the behavior of the program. Generally, the programmer uses them to include short explanations or observations within the source code itself.

#include <iostream>

The preprocessor are directed by the hash sign (#). Generally some lines are begins with # symbol at the top of the source code. In this case the directive #include<iostream> tells the preprocessor to include the iostream standard file. The iostream file includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

using namespace std;

The namespace consists of all the element belongs to standard C++ library. The namespace is called by std. So, if we like to use the functionality belongs to std, we must declare the above mention expression as like:  using namespace std;
The expression must be ended with semicolon (;). It tells the C++ compiler to apply the prefix std::
After using that prefix, names will be usable those need prefixes. It allows us to use cout in place of std::cout. This makes larger programs easier to read.

int main ()

This line indicates that main function started. All C/C++ program need main() function to start the program body. C/C++ program must have a main () function and under it all the lines of source code will be enclosed by an opening and closing curly braces ({}). The required parenthesis that follows the word “main” indicates that it is a function.  The keyword int is the name of data type in C++. It stands for “integer”. It is used here to indicate the return type for the main() function.

cout << "Hello C++ Programming";

The above line is a perfect C++ statement.  The cout represents to the standard output stream in C++ and its duty is to print/show output to the user’s screen. Here, it says to send the string “Hello C++ Programming” to the standard output stream. The single symbol << represents the C++ output operator. The characters enclosed in quotation marks "" are called string. This displays on the output device, which is usually the computer screen. It usually happened when any statement executes.  The last two characters \n represents the newline character. When the output device encounters that character, it advances to the beginning of the next line of the next on the screen. Finally, note that every program statement must end with a semicolon (;)

return 0;

Return is optional for the main () function in Standard C++. We include it here only because some compilers expect it to be included as the last line of the main () function. “Return 0” means main() will not return anything to any other function. “Return 1” means main() function will return some value according to data type mentioned in main() function prototype declaration section. Here “1” means true and “0” means false.