Table of Contents
A C++ program begins execution from the main() function. Statements inside this function are executed sequentially until the program terminates or returns from main().
This example demonstrates the basic structure of a C++ program and uses std::cout to display a message on the standard output stream.
Source Code #
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
return 0;
}
Output #
Hello, World!
Explanation #
#include <iostream>includes the standard input/output stream library. It provides objects such asstd::cout,std::cin,std::cerr, andstd::clog.int main()defines the program entry point. Program execution begins from this function.{ }defines the body of themain()function. All statements belonging to the function are enclosed within these braces.std::coutis the standard output stream object used to write data to the console.- The insertion operator (
<<) sends the string"Hello, World!"to the output stream. '\n'inserts a newline character, causing subsequent output to begin on the next line.return 0;ends themain()function and returns the value0to the operating system. A return value of0conventionally indicates successful program execution.