In this guide we will compose and understand the first program in C++ programming. We are composing a straightforward C++ program that prints “Hello World!” message. Lets see the program first and then we will talk about every single part of it in detail.
First Hello World Program in C++
/* * Multiple line * comment */ #include//Single line comment using namespace std; //This is where the execution of program begins int main() { // displays Hello World! on screen cout<<"Hello World!"; return 0; }
Hello World!
Lets talk about every single piece of the above program.
// This is a single line comment /* This is a multiple line comment * suitable for long comments */
Comments as the names proposes are only a text composed by programmer during code development. Comment doesn’t influence your program logic in any way, you can compose anything you want in comments however 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 remark.
For example:
/* This function adds two integer numbers * and returns the result as an integer value */ int sum(int num1, int num2) { return num1+num2; }
Now if someone reads my comment he or she can get what I did there just by perusing my remark. This improves comprehensibility of your code and when you are working on a project with your team mates, this becomes fundamental viewpoint..
2. #include<iostream> – This assertions tells the compiler to incorporate iostream document. This file contains pre defined input/output functions that we can utilize in our program.
3. using namespace std; – A namespace is like a region, where we have functions, variables and so on and their scope is restricted to that specific region. Here std is a namespace name, this advises the compiler to look into that particular region for all the variables, functions, etc. I will not talk about this in detail here as it may confuse you. I have covered this point in a separate tutorial with examples. Just follow the tutorial in the given sequence and you would be fine.
4. int main() – As the name proposes this is the primary function of our program and the execution of program starts with this function, the int here is the return type which shows to the compiler that this function will return a integer value. That is the main explanation we have a return 0 statement at the end of main function.
5. cout << “Hello World!”; – The cout object belongs to the iostream file and the purpose of this object is to display the substance between twofold quotes as it is on the screen. This object can likewise display the value of variables on screen(don’t worry, we will see that in the coming tutorials).
6. return 0; – This statement returns value 0 from the main() function which demonstrates that the execution of main function is successful. The value 1 represents failed execution.
0 Comments
🙏🙏please don't enter any spam links on the comment box.....