The First Program


The first C++ program

Our first C++ program will print out “I am learning C++.“(lets give “Hello world!” a break) on the monitor.Open your compiler mine is code::blocks and create a new project name “First program” and copy and paste any of the following code below.

Code example 1

#include <iostream> //Including iostream library file
 
using namespace std;
 
int main( int argc , char *argv[ ] )
{
cout<< “I am learning C++.\n” ; //Printing out on monitor

cin.get( );
return 0 ;
}

Or

Code example 2

#include <iostream> //Including iostream library file

int main( int argc , char *argv[ ] )
{
std::cout<< “I am learning C++.” << std::endl ; //Printing out on monitor

std::cin.get( );
return 0;
}

Running the above code will produce “I am learning C++.” on the screen with black background.If you don’t,check for an error in the code or any error given by the compiler.If you get it,be happy .Now,the next question which comes to our mind is “What are these?“.
 

Here are some introductory notes from our first program.

i) The obvious difference between the first and the second program is, the second program has no “using namespace std;” statement and “std::” is added in front of cout , endl and cin.get(). Here std means standard and what happens is that in the first program the addition of the statement “using namespace std;” omits the need for addition of extra “std::“. We will discuss more about this in the chapter.

 

ii) The first line #include <iostream> will include a file name iostream (also know as header’s file ) in your program while compiling.This file deals with the input and output on the monitor.Here, cout<< , << and cin.get( ) are related to iostream file.To know more about header’s file go to Libraries and how to make .h and .cpp file.

 

iii) The code execution of this program starts from the statement “ int main(int argc,char *argv[]) “.This statement has three parts –int , main() and int argc,char *argv[] .Here ,int stands for integer and main is a name given to certain region of this program(also known as scope) and after the code present inside main is executed it will return a value to the OS.What type of value returned is signified by int present in front of the main() so,in this case an integer will be returned.The last part int argc,char *argv[] ,signify what is passed to main() .So, an integer with a name argc(known as variable discussed in chapter 1) and an array of char( character) pointer (discussed in chapter 2) is passed to main().

 

iv) In the previous explanation I have said that main() is a name of certain region ,after int main(int argc,char *argv[]) you will see ‘{‘ also known as opening braces and this symbol signify the starting of the region defined by main() with ‘}closing braces as the end of the region.

 

v) The line “return 0;” before ‘}’ (closing braces) marks the end of the code under main().The value return by main() will be written here.In this case a value 0(zero) is returned and is an integer as signified by int present in front of main() .

 

vi) The “ cout ” stands for console output and “cin” stands for console input.When we were starting out to make out first program in our compiler we chose console application.So every output that we want in our console application is obtained using cout<< .So whatever follows under “”(double quotation ) after “cout<<” and “<<” is printed out on the monitor screen.The cin.get(); allows you to input a character from the keyboard. The “endl” and “ \n “(of C) function is to print out the next output in the next line.The “;“(semi colon) signify the end of every statement so missing it out will give you “Syntax error“.

 

vii) Now go to the directory where your First program.exe file is present,in my case the directory is “C:\My_program\Hello\bin\Debug“.Double click on the First program.exe file.You will see “I am learning C++.“.Such file(.exe file) serve as an interface for all the program present in your software.Now transfer this file to another laptop/computer and run it.You would still be getting “I am learning C++“.