This tutorial describes the C++ program structure. Here's what you need to know before learning more advanced C++ programming concepts.



A C++ program consists of the following parts:
  • Documentation
  • Preprocessor Statements
  • Global Declarations
  • The main() function
    • Local Declarations
    • Program Statements and Expressions
  • User Defined Functions

Let's begin with a simple C++ program code.

C++ Program That Outputs a Line of Text

/* 
* File: main.cpp
* Author: Gautam
* Created on April 28, 2018
*/ 

#include <iostream> 

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

Program Output:

first-cplusplus-program

The above example prints the text on the screen.

Let's look into various parts of the above C++ program:

/* Comments */ Comments are a way of explaining what makes a program. The compiler ignores comments, and others use it to understand the code.

or

The C++ compiler ignores the comment block. Comments can be used anywhere in the program to add information about a program or code block, which will be helpful for developers to understand existing code in the future quickly.

#include <iostream> It is a preprocessor directive. It tells the preprocessor to include the contents of the iostream header file in the program before compilation. This file is required for input-output statements.
int/void 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 must contain only one main function.

or

It is the main function, the default entry point for every C++ program, and the void in front of it indicates that it does not return a value.

Braces Two curly brackets "{…}" are used to group all statements.

or

Curly braces show how much the main() function has its scope.

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 the C++ language, the cout and << operators.

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 grouped characters is called a string. Strings must always be placed between inverted commas.

We used std:: before cout. It is required when we use #include <iostream>. It specifies that we use a name cout that belongs to the namespace std. A namespace is a new concept introduced by ANSI C++ that defines the scope of identifiers used in the program. std is the namespace where C++ standard libraries are defined.

Operator << is the insertion stream operator. It sends the variable's contents on its right to the object on its left. In our case, the right operand is the string "This is my first c++ Program" and a left operand is a cout object. So it sends the string to the cout object, and the cout object then displays it on the output screen.

namespace

using namespace std;

If you specify using namespace std, 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.

Example:

/* 
* 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<<std::endl<<"and its very easy";
}

Return Statement

return 0 At the end of the main function returns the value 0.
In new C++, you have to use:
  • int main() instead of void main()
  • After you import your headers, you required to use using namespace std;
  • There is no header file like iostream.h, you are only required to use this as #include <iostream>

void main() and iostream.h is only valid for Turbo C++.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram