Home | API | MFC | C++ | C | Next

Programming With C++

The Basics

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. Preprocessor directives are always preceded by the character #. The preprocessor’s job is to read source code looking 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 that is needed for reading from the keyboard and writing information to the screen.

The angled brackets (< >) around the filename iostream tells 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, which are used to pass parameters from the calling environment.

Braces{}

Brackets or brace marks are used to group together 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 scope of the function.

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 C++ Standard Library is a collection of classes and functions, which are included as part of the standard core language.  The standard output operator << causes whatever expression is on its right side to be output to the device specified on its left side. You can use the output operator 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 that are not evaluated by the compiler and are used by programmers to explain their code. Comments are either 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 individual 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 if terminated with a semi colon.


Home | API | MFC | C++ | C | Next
The Basics | Variables and Constants | Arrays | C-strings | Expressions and Operators | Controlling Program Flow | C++ Functions | Pointers and References | Memory Map and Free Store | Smart Pointers | Classes | Structures | Inheritance | Polymorphism | Templates | The Standard Template Library | The STL String Class | Namespace | Type Conversions | Input and Output Streams | The C++ Preprocessor | Exception Handling

Last Updated: 15 September 2022