C++ Program Structure
Preprocessor Macro __TIME__
Expands to the time at which the compilation is taking place.
#undef
Simply removes the definition of an identifier in the preprocessor that was previously defined with #define.
#ifndef #define #endif
When header files are included by other header files there is a risk that a file maybe included more than once. These preprocessor directives are used to avoid this happening by wrapping the header files declarations and definition with a condition that checks the definition of an identifier.
preprocessor macros Vs inline functions & templates
inline functions and templates are preferred to try and avoid unexpected results. Macros use test substitution so the results are inconsistent with compiled logic.
preprocessor #
signifies a preprocessor directive, as long as it is the first character on the line apart from whitespace.
Object Files & Linking
Object files are joined by the linker to produce either an executable or a library.
Header Files and Class Definitions
Often there is one header file for each class definition but headers can be used for other purposes and classes can be defined within a source file too (e.g. helper classes only needed in the scope of that source).
#define Vs enumerations, statics and consts
Preferable to use enumerations, static and const variables to use identifiers for constant values instead of #define.
Program Termination
Program terminates when returning from main or calling the 'exit' function from the C++ standard library. The return value from the program is returned from main (an int) or supplied as part of the 'exit' function.
#pragma
Signifies the use of a non-standard preprocessor directive but is available in the current C++ implementation. E.g. 'message("Logging Enabled")' is a directive available in MS Visual C++ 6.0 used to display a message during compilation.
Source Files & Compilation
The compiler translates source files into object files(.obj, .o).
#include
A preprocessor directive to include the contents of another file within the current translation unit. Usually this will be a header file, either with a .h extension or no extension (standard library headers).
Header Files
Contain source code to be included in multiple places. Shouldn't include definitions for variables or static data members or any functions except inline functions or template functions. Also shouldn't included namespaces that are un-named.
Source Files
Contain source code, compiled individually but may 'include' code from header files via the pre-processor before compilation. Usually contain the implementation of classes. Usually have a .cpp extension.
#ifdef, #ifndef, #else, #endif
Control what code is included in compilation by checking for the definition of a given identifier. These directives are used together, starting with either #ifdef or #ifndef, using #else optionally to select between code to include, and always ends with #endif
__cplusplus
Defined if this is a C++ program (as opposed to C), perhaps because a compiler option is set.
Translation Unit
Describes the results of a source file after it has been modified by the pre-processor.
#if, #elif, #else, #endif
Directives that are used together, where #if and #elif use the result of a logical expression (<>==&&||!) to test whether source code is included or not and #else to select, always finalised by #endif.
preprocessor #define
Either replaces a specified identifier with the text that follows or simply defines the identifier if there is no text. E.g. '#define JIM 50' would direct the preprocessor to replace all entries of 'JIM' in the source code with the text '50'. #define [identifier] on its own simply defines the identifier within the preprocessor but expands it to nothing and doesn't include it in the translation unit.
Preprocessor Macro __LINE__
Expands to the current line number of the source file being compiled.
Preprocessor Macro __TIMESTAMP__
Expands to the date and time at which the compilation is taking place.
Preprocessor Macro __DATE__
Expands to the date on which the compilation is taking place.
Preprocessor Macro __FILE__
Expands to the name of the source file being compiled.
Standard Library Headers
Although generally header files have the .h extension, standard library header files have *no extension*.
Program Entry Point
Always 'main' which is a function that returns an int and can take either zero or 2 parameters. When it has parameters, the values are supplied by the OS; first parameter is the number of arguments that have been passed in via the second parameter which is an array of character pointers (often containing arguments supplied via a command line).
argv[0]
As passed from the OS to the main method entry point in a C++ program, this first element in the array contains a character pointer to an array of characters containing the name of the executable.
#include "" or <>
Includes another file, usually a header. User-defined header files use quotes enclosing an path and filename. When using the <> angled brackets, this uses standard header files which are looked for elsewhere.
#define, arguments and macro substitution
It is possible to pass arguments to a define directive which results in macro substitution. E.g. '#define MIN(a, b) (((a) < (b)) ? (a) : (b))'. Where the use of the identifier MIN(textA, textB), where a and b are substituted in the rest of the macro with the given text . Note the use of *text substitution*, the arguments are not evaluated like the arguments in a function call. Ie. MIN(x++, y--) would be expanded to '(((x++) < (y--)) ? (x++) : (y--))' in the source code which may not be what was required.
#error
Preprocessor directive that causes compilation to stop and display the provided string. '#error This code shouldn't be reached'.
#line
Preprocessor directive that changes the line number and, optionally, filename stored during compilation and used with the __LINE__ and __FILE__ Macros. E.g. '#line 100 "NewName.cpp".
Preprocessor Macro __STDC__
Will be defined if the compiler is in full compliance with the ANSI C standard.
preprocessor \
backslash is used to extend a preprocessor directive definition beyond a single line.