Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

3.2 Hello, world!

The minimal C++ program is

int main() { }

It defines a function called main, which takes no arguments and does nothing.

Every C++ program must have a function named main(). The program starts by executing that function. The int value returned by main(), if any, is the program's return value to ''the system.'' If no value is returned, the system will receive a value indicating successful completion. A nonzero value from main() indicates failure.

Typically, a program produces some output. Here is a program that writes out Hello, world!:

#include <iostream>
int main()
{
  std: :cout << "Hello, world!\n";
}

The line #include <iostream> instructs the compiler to include the declarations of the standard stream I/O facilities as found in iostream. Without these declarations, the expression

std: :cout << "Hello, world!\n"

would make no sense. The operator << (''put to'') writes its second argument onto its first. In this case, the string literal "Hello, world!\n" is written onto the standard output stream std: :cout. A string literal is a sequence of characters surrounded by double quotes. In a string literal, the backslash character \ followed by another character denotes a single special character. In this case, \n is the newline character, so that the characters written are Hello, world! followed by a newline.

  • + Share This
  • 🔖 Save To Your Account