C++ Program - Hello World
Hello World
Here is a simple C++ program that displays "Hello World!":
#include <iostream>
This line includes the iostream header file, which allows us to use the cout object to output text to the console.
int main()
This line declares the main function of the program. The program execution begins from the main function.
{
The opening curly brace indicates the beginning of the main function block.
std::cout << "Hello, World!";
The cout object, which is a part of the iostream library, is used to output the text "Hello, World!" to the console. The << operator is used to insert the text into the cout object.
return 0;
This line returns a value of 0 from the main function to indicate that the program has executed successfully.
}
The closing curly brace indicates the end of the main function block.
Using namespace std ;
Note that using namespace std; can be added before the main function to avoid using std:: before cout and other standard libraries.
Using cout Object Directly Without the std:: Prefix
Here, using namespace std; is added before the main function to avoid using std:: before cout and other standard libraries. This means that the cout object and other standard library names can be used directly without the need to qualify them with the std:: prefix.
Don't Use namspace std; in Large-Scale Projects
Note that it is not recommended to use namespace std; in large-scale projects or in code that will be reused or distributed to others, because it can cause naming conflicts.
Image by 200 Degrees from Pixabay
Comments
Post a Comment