Hello World
The header file std_lib_facilities.h
is provided to simplify the use of standard library facilities. It is used with #include
directive to make them available in the code.
// This program outputs the message "Hello, World!"
#include "../std_lib_facilities.h"
int main() // C++ programs start by executing the function main
{
cout << "Hello, World!\n";
return 0;
}
In C++, string literals are delimited by double quotes ("
) from both sides. The \n
is a special character indicating a new line. The name cout
refers to a standard output stream. Characters are forwarded to cout
stream using the output operator <<
. A comment line starts with the token //
.
Every C++ program must have a function called main
to tell it where to start executing the code. A function has the following parts:
- A return type, here int
.
- A name, here main
.
- A parameter list enclosed in parentheses, here ()
.
- A function body enclosed in curly braces {}
lists the actions for the function to execute. Such actions are called statements.
In the above program, the main()
function returns the value 0
to whoever called it. Since main()
is called by the system, we don't use this return value. However, in Unix, a zero value returned by main()
indicates that the program terminates successfully.
C++ is a compiled language. It first translates human-readable code to machine instructions called object code by a program called a compiler. Then a program called a linker combines machine instructions from separate parts (for example, the code we have written and the parts from C++ standard library) into an executable program. The executable programs cannot be used across different operating systems.
There are compile-time errors found by the compiler, link-time errors found by the linker, and run-time errors found when the program is run. Debugging is the activity of finding errors in a program and removing them.