A Simple Piece of C++ Code
#include <iostream> //Preprocessor directive
int main() // Start of your program: function block main()
{ //Opening brace
std::cout << "Hello World" << std::endl; // Write to the screen
return 0; // Return a value to the OS
} //Closing brace
#include <iostream>
The first line in the code is called a preprocessor directive. The character always precedes preprocessor directives. The preprocessor processes these directives before the source code is passed to the compiler.#
The #include <iostream> directive tells the preprocessor to include the contents of the standard C++ iostream header. This header provides declarations for C++ stream-based input and output, including objects such as , std::cout, and std::cin.std::cerr
The angled brackets (< >) around the header name tell the compiler to search for the header using the implementation’s standard include paths. Double quotation marks (" ") are normally used when including a header from a project or other location specified by the implementation’s include search rules.
C++ includes a standard library of classes, functions, objects, and other facilities defined by 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{}
Braces are used to group statements into a block (also called a compound statement). In a function definition, the opening brace { marks the beginning of the function body and the closing brace } marks its end.
Everything between the opening and closing braces forms part of the function body.
For example:
{
std::cout << "Hello World";
}
Braces are also used in many other parts of C++, including , if, and forstatements, as well as classes and other constructs.while
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 single lines or 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 end 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.