Dec 23, 2014

General structure of a c++ program


General structure of a c++ program :

#include <iostream>
using namespace std;
int main() {
cout << "This is a simple C++ program!" << endl;
}


1. #include <iostream>
     
                       This line is a preprocessing directive. All preprocessing directives within C ++ source code begin with a # symbol. This one directs the preprocessor to add some predefined source code to our existing source code before the compiler begins to process it. This process is done automatically.

IOSTREAM :

               IOSTREAM library, a collection of precompiled C ++ code that C ++ programs (like ours) can use. The iostream library contains routines that handle input and output (I/O) that include functions such as printing to the display, getting user input from the keyboard. These items, along with many other things related to input and output, were developed in C ++ , compiled and stored in the iostream library.

               
             The #include directive specifies a file, called a header that contains the specifications for the library code. The compiler checks how we use cout and endl within our code against the specifications in the <iostream> header to ensure that we are using the library code correctly.

2. Using namespace std ;
        
                  This using namespace directive will allow us to omit , from writing the longer name std::cout and std::endl instead we can write directly as cout and endl.It's not mandatory to use this statement in every program.It makes simple and easy to use the shorter names(cout,endl..etc).

            std::cout << "This is a simple C++ program!" << std::endl;

3. int main() {
           
              This specifies the real beginning of our program. Here we are declaring a function named main. All C ++ programs must contain this function to be executable.

            Most of them are thinking why we have to use int as return for main function..? Here is answer for u r doubt.

           The return value from the main function is used by the run time library as the exit code for the process. Both Unix and Windows support a concept of a integer returned from a process after it has finished. 

           The body of the main function does not need to contain the return statement , if control reaches the end of main without encountering a return statement , the effect is that of executing the return 0 ; Execution of the return ( or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return ( std::exit then destroys static objects and terminates the program). 
 
            The opening curly brace represents the begining of the body of the function.

4. cout << "This is a simple C++ program!"<< endl;
           
            The body of our main function contains only one statement. This statement directs the executing program to print the message “This is a simple C++ program!” on the screen. A statement is the fundamental unit of execution in a C ++ program.All statements in C ++ end with a semicolon ;

5. }

             The closing curly brace marks the end of the body of a function. Both the open curly brace and close curly brace are required for every function definition.

Filed under  |