Datasheet

The table below shows some of the most common preprocessor directives.
Preprocessor Directive Functionality Common Uses
#include [file] The specified file is inserted into Almost always used to include
the code at the location of the header files so that code can
directive. make use of functionality that
is defined elsewhere.
#define [key] [value] Every occurrence of the specified Often used in C to define a
key is replaced with the specified constant value or a macro. C++
value. provides a better mechanism
for constants. Macros are often
dangerous so
#define is rarely
used in C++. See Chapter 12
for details.
#ifdef [key] Code within the ifdef Used most frequently to protect
#ifndef [key] (“if defined”) or ifndef against circular includes. Each
#endif (“if not defined”) blocks are included file defines a value
conditionally included or initially and surrounds the rest
omitted based on whether of its code with a
#ifndef and
the specified value has been
#endif so that it won’t be
defined with
#define. included multiple times.
#pragma Varies from compiler to compiler. Because usage of #pragma is not
Often allows the programmer to standard across compilers, we
display a warning or error if the advocate not using it.
directive is reached during
preprocessing.
The main function
main() is, of course, where the program starts. An int is returned from main(), indicating the result
status of the program.
main() takes two parameters: argc gives the number of arguments passed to the
program, and
argv contains those arguments. Note that the first argument is always the name of the
program itself.
I/O Streams
If you’re new to C++ and coming from a C background, you’re probably wondering what std::cout is
and what has been done with trusty old
printf(). While printf() can still be used in C++, a much
better input/output facility is provided by the streams library.
In C, included files usually end in .h, such as <stdio.h>. In C++, the suffix is omit-
ted for standard library headers, such as
<iostream>. Your favorite standard head-
ers from C still exist in C++, but with new names. For example, you can access the
functionality from
<stdio.h> by including <cstdio>.
3
A Crash Course in C++
04_574841 ch01.qxd 12/15/04 3:39 PM Page 3