Online Book Reader

Home Category

Professional C__ - Marc Gregoire [2]

By Root 1063 0
you’re a C programmer, be on the lookout for new or unfamiliar syntax!

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 the previous example. In this case, an include directive tells the preprocessor to take everything from the header file and make it available to the current file. The most common use of header files is to declare functions that will be defined elsewhere. Remember, a declaration tells the compiler how a function is called. A definition contains the actual code for the function. The header declares the input and output mechanisms provided by C++. If the program did not include it, it would be unable to perform its only task of outputting text.

In C, included files usually end in .h, such as . In C++, the suffix is omitted for standard library headers, such as . Your favorite standard headers from C still exist in C++, but with new names. For example, you can access the functionality from by including .

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

Return Main Page Previous Page Next Page

®Online Book Reader