in this guide we will write and understand first program in C++ programming.
We are writing a simple C++ program that prints “Hello World!” message. Lets see the program first and then we will discuss each and every part of it in detail.
Hello World Program in C++
#include <iostream> // its compulsory for write a code in C++ programmig
using namespace std; //this is where program execution begin
int mainn () {
// main function start here
cout << Hello World!; // to print out hello world! on screen
system ("PAUSE"); // this statement use to pause at result which will be executed
return 0; // to return zero after execution of program its not visible but its work in back end
}
Output of the Code written is;
Hello World!
press any key to exit..........................
To add a comment in code you are writing:
there are two types of comment
1. sigle line comment
2. Multi line comment
Single line Comment; A single line comment written as // your comment here
Multi Line comment; multi line comment written as /* your comment statement here */
1. #include<iostream> – This statements tells the compiler to include iostream file. This file contains pre defined input/output functions that we can use in our program.
2. using namespace std; – A namespace is like a region, where we have functions, variables etc and their scope is limited to that particular region. Here std is a namespace name, this tells the compiler to look into that particular region for all the variables, functions, etc. I will not discuss this in detail here as it may confuse you. I have covered this topic in a separate tutorial with examples. Just follow the tutorial in the given sequence and you would be fine.
3. int main() – As the name suggests this is the main function of our program and the execution of program begins with this function, the int here is the return type which indicates to the compiler that this function will return a integer value. That is the main reason we have a return 0 statement at the end of main function.
4. cout << “Hello World!”; – The
cout
object belongs to the iostream file and the purpose of this object is to display the content between double quotes as it is on the screen. This object can also display the value of variables on screen(don’t worry, we will see that in the coming tutorials).
5. return 0; – This statement returns value 0 from the main() function which indicates that the execution of main function is successful. The value 1 represents failed execution.
No comments:
Post a Comment