Professional C__ - Marc Gregoire [2]
The Obligatory Hello, World
In all its glory, the following code is the simplest C++ program you’re likely to encounter.
// helloworld.cpp
#include int main() { std::cout << "Hello, World!" << std::endl; return 0; } Code snippet from helloworld\helloworld.cpp This code, as you might expect, prints the message “Hello, World!” on the screen. It is a simple program and unlikely to win any awards, but it does exhibit the following important concepts about the format of a C++ program. Comments Preprocessor Directives The main() Function I/O Streams These concepts are briefly explained in the next sections. Comments The first line of the program is a comment, a message that exists for the programmer only and is ignored by the compiler. In C++, there are two ways to delineate a comment. In the preceding and following examples, two slashes indicate that whatever follows on that line is a comment. // helloworld.cpp The same behavior (this is to say, none) would be achieved by using a C-style comment, which is also valid in C++. C-style comments start with /* and end with */. In this fashion, C-style comments are capable of spanning multiple lines. The following code shows a C-style comment in action (or, more appropriately, inaction). /* this is a multiline * C-style comment. The * compiler will ignore * it. */ Comments are covered in detail in Chapter 5. Preprocessor Directives Building a C++ program is a three-step process. First, the code is run through a preprocessor, which recognizes meta-information about the code. Next, the code is compiled, or translated into machine-readable object files. Finally, the individual object files are linked together into a single application. Directives aimed at the preprocessor start with the # character, as in the line #include In C, included files usually end in .h, such as The following table shows some of the most common preprocessor directives. PREPROCESSOR DIRECTIVE FUNCTIONALITY COMMON USES #include [file] The specified file is inserted into the code at the location of the directive. Almost always used to include header files so that code can make use of functionality defined elsewhere. #define [key] [value] Every occurrence of the specified key is replaced with the specified value. Often used in C to define a constant value or a macro. C++ provides a better mechanism for constants. Macros are often dangerous so #define is rarely used in C++. See Chapter 9 for details. #ifdef [key] #ifndef [key] #endif Code within the ifdef (“if defined”) or ifndef (“if not defined”) blocks are conditionally included or omitted based on whether the specified key has been defined with #define. Used most frequently to protect against circular includes. Each included file defines a value initially and surrounds the rest of its code with a #ifndef and #endif so that it won’t be included multiple times, see example after this table. #pragma [xyz] xyz varies from compiler to compiler. Often allows the programmer to display a warning or error if the directive is reached during preprocessing. See example after this table. One example of using preprocessor directives