/    /  CPP- Simple C++ Program

A Simple C++ Program

 

A simple example of a C++ program that displays a string on your screen.

#include <iostream> //header file
using namespace std; // namespace
int main() //main function
{
cout<<"Welcome to C++ language \n"; // printing statement
 return 0;
} //end of program

 

Output:

A Simple C++ Program

 

This program demonstrates several C++ features.

 

Features of a C++ Program

Like C, the C++ program is a collection of functions such as main() as in the above example. Every program must have a main() and execution begins from main(). C++ is a free-form language. Like C, the C++ statements terminate with a semicolon.

 

Comments:

C++ introduces a new comment symbol //(double slash). Comments start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in the program and whatever follows till the end of the line is ignored. There is no closing symbol.

 

Example 1:

//this is an example program
// for C++ learners

The C comments /* ….. */ are also valid and are more suitable for multi line comments.

 

Example 2:

/* this is an example program
For C++ learners */

 

Note: We can use either or both styles in our programs.

 

The iostream File:

The first line in the program

 

# include <iostream.h>

 

Causes the preprocessor to add the contents of the iostream file to the program. The iostream file contains declarations for the identifiers ‘cout’ and ‘cin’ and the operators ‘<<’ and‘>>’. The file should be included at the beginning of all programs that use input/output statements.

 

Return Type of main ( ):

In C++, main() returns an integer type value to the operating system. Therefore, every main() in C++ should end with a return (0) statement, otherwise, a warning or an error might occur. Since main() returns an integer type value, return type for main() is explicitly specified asint.

 

Example:

int main( ) //main return integer
{
----------;
-----------;
-----------;
return 0; //return zero statement
}