A Simple Piece of C++ Code
#include <iostream> //Preprocessor directive
int main() // Start of your program: function block main()
{ //Opening bracket
std::cout << "Hello World" << std::endl; // Write to the screen
return 0; // Return a value to the OS
} //Closing bracket
#include <iostream>
The first line in the code is called a preprocessor directive. The character # always precedes preprocessor directives. The preprocessor’s job is to search source code for directives and modify the code according to the indicated directive. The #include <iostream> directive tells the preprocessor that what follows is the name of a file from the standard library. The file contents are read into the program and the modified code is then fed to the compiler for compilation. The <iostream> header includes important code needed for reading the keyboard and writing information to the screen.
The angled brackets (< >) around the filename iostream tell the preprocessor to look in an explicitly specified compiler-specific location for the file. Using double quotes instead of angled brackets indicates that the current working directory should be searched for the required header file. C++ includes a standard library of classes and functions that are part of the C++ ISO Standard.
Int main()
Every C++ program includes the function int main() and can have only one main() function. When a program starts, main() is called automatically and marks the entry point for the program. Main() cannot be called from within a program. The body of the main() function does not need to contain a return statement; if control reaches the end of the main block the effect is that of a return 0. Main() can also accept two command line arguments: argv and argc used to pass parameters from the calling environment.
Braces{}
Brackets or brace marks are used to group blocks of code. All functions begin with an opening brace { and end with a closing brace }. Everything between the opening and closing braces is part of the function scope.
Std::cout
Cout stands for console out and is used to write simple text data to the console. The designation std:: instructs the compiler to use the standard C++ input/output library. The standard output operator << causes whatever expression is on its right side to be output to the device specified on its left side. The output operator can be used multiple times in a single cout statement to display separate character strings.
Return 0
Terminates main( ) and causes it to return the value 0 to the calling process which is typically the operating system. Functions in C++ need to return a value unless explicitly specified otherwise. Function main() always returns an integer. A return value of 0 signifies that the program is terminating normally. Other values indicate that the program is terminating because of some error. All C+ programs should return 0 when they terminate normally.
Comments
Comments are lines of text not evaluated by the compiler and are used by programmers to explain their code. Comments are either a single line or can enclose blocks of text.
A single-line comment begins with two slash marks ( // ) and causes the compiler to ignore everything that follows the slashes on the same line as follows -
//this is a comment
A multiple-line comment begins with the backslash and asterisk character ( /* ) and ends with the same characters reversed ( */ ). Everything enclosed is a comment -
/*
this is a comment
*/
Semicolons and Positioning
In C++, the semicolon is a statement terminator. Each statement must be ended with a semicolon since C++ does not recognise the end of the line as a terminator. A block is not terminated with a semicolon as a block is used to group statements. Several statements can be included on one line as long as each statement is terminated with a semicolon.