Learning C++: First C++ program or “Hello world” program in C++

Pages

Thursday, August 18, 2011

First C++ program or “Hello world” program in C++


Here is the first C++ program or simplest of all C++ program. While writing program, first its structure is required to understand, structure means its “PROGRAM FLOW”.
There is some steps of C++ which needs to be follow while writing C++ program, these steps are as follows....

1) Including header files or namespaces:
Including of header files for C++ program is compulsory. Header files are basically predefine functions which simplifies complexity of code.
Example: To display character on screen in C++ program it requires number of lines of code, but with the help of <iostream.h> it’s very simple as to write only keyword cout<<”character to display”. 
      
 2) Writing main function:
Now what is an importance of main function? Main function is starting point for any C++ program. Compiler starts reading code from main function. Main function for C++ is written as void main() or int main()




     3)  Declaring variables
Many programs needs operations of entities, like addition of numbers, here it will require variables (Space in program) for numbers.
In C++ we can declare variable anywhere in program but I recommend declaring it earlier in program.

       4) Operations
Next step is operations over variable declared as per requirement of program.

       5) Commenting
Comment is most important part of any program, because a good programmer never forgets to comment its code. Comment is basically statement written in natural language to understand the code. Its starts with // sign. It can be written anywhere in program as per programmer’s wish.
Compiler never reads code after // sign, so it has no effect on code, but it increases understandability of code to other than you. 


Program to display word “Hello world”

// Including header file


#include<stdio.h>

#include<iostream.h>



//start of main function


void main()

{


// Displaying message under double braces
 

cout << “Hello world ” ; // cout is keyword


//require to sudden stop of program, getch gets character


getch();



Try this program once with turbo C IDE. Comment me for any doubt or query.
Enjoy programming :) 

No comments:

Post a Comment