Customizable training materials to learn C++

Nano teach C++ found result for your search here:

C++ Program Structure

This tutorial will explain you about the C++ program structure.
Basically a C++ program involves the following section.
  • Documentations
  • Preprocessor Statements
  • Global Declarations
  • The main() function
    • Local Declarations
    • Program Statements & Expressions
  • User Defined Functions

Here is a simple program which outputs a line of text.


* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 4:09 PM

    #include <iostream>

    int main() {
    std::cout<<"This is my first c++ Program.";
    std::cout<<std::endl<<"and its very easy"; 

Program Output:
This is my first c++ Program.
and its very easy

Let’s look into various parts of the above C++ program.

/* Comments */ Comments are a way of explaining what makes a program. Comments are ignored by the compiler and used by others to understand the code.
#include<iostream> is a preprocessor directive. It tells the preprocessor to include the contents of iostream header file in the program before compilation. This file is required for input output statements.
int/void main() int/void is a return value, which will be explained in a while.
main() The main() is the main function where program execution begins. Every C++ program should contain only one main function.
Braces Two curly brackets “{…}” are used to group all statements together.

std::cout<<”This is my first c++ Program”;

The above line is a statement in C++. A statement must always terminate with a semicolon (;) otherwise it causes a syntax error. This statement introduces two new features of C++ language, cout and << operator.
You will also notice that the words are inside inverted commas because they are what is called a string. Each letter is called a character and a series of characters that is grouped together is called a string. Strings must always be put between inverted commas.
We used std:: before cout. This is required when we use #include <iostream> .
It specifies that we are using a name (cout) which belongs to namespace std. Namespace is a new concept introduced by ANSI C++ which defines the scope of identifiers which are used in the program. std is the namespace where C++ standard libraries are defined.
Operator << is the insertion stream operator. It sends contents of variable on its right to the object on its left. In our case, right operand is the string “This is my first c++ Program” and left operand is cout object. So it sends the string to the cout object and cout object then displays it on the output screen.


using namespace std;

If you specify using namespace std then you don’t have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.

* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 4:09 PM

    #include <iostream>
    using namespace std; 

    int main() {  
    cout<<"This is my first c++ Program.";
    cout<<endl<<"and its very easy";

Return Statement

return 0 At the end of the main function returns value 0.

No comments:

Post a Comment