First C++ Program

Previous
Next
  • This Document clearly explains how to write your First C++ program what will display “Hello World” Message on the Monitor.
  • We will also discuss about single line and multi line comments as well
  • Look at the following program
/*
It is
Multi-Line
Comment
In C++
Used for program explanation.
*/

// It is single line comment – used for line description

#include<iostream>
using namespace std;  
int main()
{
        cout<<"Hello World!";  // “cout” display message on the monitor
   	return 0;
}

Comments:

  • Programmer use comments for line description or entire document description during development.
  • Comment doesn’t affect your program logic in any way, you can write whatever you want in comments but it should be related to the code and have some meaning so that when someone else look into your code, the person should understand what you did in the code by just reading your comment.
  • Single line comments always describe the same line
  • Multi line comments always describe a block of code or entire program

Header files:

  • #include is a preprocessor command that that is used to connect your source file with libraries which are represented in angular brackets(<>)
  • The file <iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams. This command contains code for displaying and getting an input from the user.
  • “Namespace” is a prefix that is applied to all the names in a certain set. iostream file defines two names used in this program – cout and endl.

Main function:

  • The starting point of all C++ programs is the main function.
  • This function is called by the operating system when your program is executed by the computer.
  • { signifies the start of a block of code, and } signifies the end.

Function body:

  cout << “Hello World” << endl;

  return 0;

  • The name cout is short for character output and displays whatever is between the << brackets.
  • Symbols such as << can also behave like functions and are used with the keyword cout.
  • The keyword return is a special statement which is used to exit a function and return a value.
  • This statement exits the function main indicating its completion and returns the value 0 to the operating system indicating that the program successfully ran to completion.
  • return statement is included at the end of every main function.
Previous
Next

Courses Enquiry Form