Write a program in C++ to print Hello World

Program : To print hello world using C++


// Your First C++ Program

#include 

int main() {
    std::cout << "Hello World!";
    return 0;
}

Output :


Hello World!

Explanation:

1. // Your First C++ Program

In C++, any line starting with // is a comment. Comments are intended for the person reading the code to better understand the functionality of the program. It is completely ignored by the C++ compiler.

2. #include

The #include is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file.

This allows us to use cout in our program to print output on the screen.

For now, just remember that we need to use #include to use cout that allows us to print output on the screen.

3. int main() {…}

A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function.

The execution of code beings from this function.

4. std::cout << “Hello World!”;

std::cout prints the content inside the quotation marks. It must be followed by << followed by the format string. In our example, “Hello World!” is the format string.

Note:  ” ; ” is used to indicate the end of a statement.

5. return 0;

The return 0; statement is the “Exit status” of the program. In simple terms, the program ends with this statement

Note : If you dont want to use std::cout you can used namespace.


#include
using namespace std;
int main()
{
     cout<<"Hello world";
     return 0;
}

Output :


Hello World!
Write a program in C++ to print Hello World

Published on 21-Dec-2021 12:06:30

You may also like this!