First Program in C++


Table of Contents

Our first C++ program is pretty simple. We will send a couple of instructions to the computer to print a message on the screen. It’s a very simple program but new programmers can learn some basic stuff that are necessary and are used in almost every single program, or we can say the building blocks of a C++ program. So start typing the following code in your favorite Code editor (IDE).

  1. // First program in C++
  2.  // Date: 21-Aug-2012
  3.  #include //output data to the screen
  4.  //Main function starts execution
  5.  int main()
  6.  { //Body of Main functions starts with the braces
  7.  cout<< “My first program in C++”;
  8.  return 0;
  9.  } // Main functions ends here

It is a good programming practice to start every program with a comment. And that comment should describe the date, the name of the programmer and the purpose of the program.

The # include is a preprocessor directive which tells the preprocessor to put the contents of the input/output header file .

Blank lines, white spaces and tabs are used for indentation purpose to make the code readable. So that when other programmers read your code they should read it easily.

Lines starting with // is a comment. Comments are is an important part of a c++ program and they should be used intelligently. That’s means a comment should explain how a line of code is going to perform a certain task.

Int main() is a function. Every C++ program must have the main() function. A c++ program starts its execution from the main function. A function body starts with the opening braces { and ends with the closing braces }. Inside the braces are statements of the function. In this program cout<


Here is the screen shot of the program. For this program i have used Notepad++ text editor. You can use any text editor.

First program in C++

Things to Remember

  • Blank lines & spaces improve program readability so use them. It won’t make any difference in a small program of few lines but think about it when you are dealing with a 2000-3000 lines of code you don’t want to be in that mess.
  • Use comments that can explain your code.
  • Every C++ program starts with the main() function. And every C++ program must have the main() function.
  • Always remember to start the body of the function with the opening braces { and at the end the closing braces }. Forgetting to put the braces will throw an error. And you also don’t want errors in your first program. 
  • Dont forget the Semicolon at the end of a statement.
Exit mobile version