Cpp - Your first C++ program

Description

Your first C++ program

Demo

#include <iostream> 
using namespace std; 

int main() //from w ww .ja  v  a 2s.c  o m
{ 
      cout <<  "hi from book2s.com!"  << endl; 
      return 0; 
}

Result

The first line begins with the number symbol, #, which indicates that the line is the preprocessor.

You can type

#include <filename>  

to have the preprocessor copy the quoted file to this position in the source code.

In this way the program can access to all the information contained in the header file.

The header file iostream has conventions for input and output streams.

Predefined names in C++ are to be found in the std (standard) namespace.

The using directive allows direct access to the names of the std namespace.

Program execution begins with the first instruction in function main().

Each C++ program must have a main function.

In our example the function main() contains two statements. The first statement

cout << "hi from book2s.com!" << endl;  

outputs the text string hi from book2s.com! on the screen.

The name cout (console output) designates an object responsible for output.

The two less-than symbols, <<, indicate that characters are being "pushed" to the output stream.

Finally endl (end of line) causes a line feed. The statement

return 0;  

terminates the function main() and the program, returning a value of 0 as an exit code to the calling program.

It is standard practice to use the exit code 0 to indicate that a program has terminated correctly.

Related Topics

Exercise