C C programmer s reference 3rd ed Edition Schildt download pdf
C C programmer s reference 3rd ed Edition Schildt download pdf
https://ebookgate.com/product/borland-c-builder-the-complete-
reference-herbert-schildt/
ebookgate.com
https://ebookgate.com/product/vbscript-programmer-s-reference-3rd-
edition-adrian-kingsley-hughes/
ebookgate.com
https://ebookgate.com/product/javascript-programmer-s-reference-1st-
ed-edition-alexei-white/
ebookgate.com
https://ebookgate.com/product/beginning-visual-c-2010-wrox-programmer-
to-programmer-1st-edition-karli-watson/
ebookgate.com
HTML5 Programmer s Reference 1st Edition Jonathan Reid
https://ebookgate.com/product/html5-programmer-s-reference-1st-
edition-jonathan-reid/
ebookgate.com
https://ebookgate.com/product/java-a-beginner-s-guide-3rd-edition-
schildt/
ebookgate.com
https://ebookgate.com/product/professional-c-4-and-net-4-wrox-
programmer-to-programmer-1st-edition-christian-nagel/
ebookgate.com
https://ebookgate.com/product/martindale-the-complete-drug-
reference-37th-edition-shawn-c-sweetman-ed/
ebookgate.com
https://ebookgate.com/product/the-fungi-3rd-ed-2015-3rd-edition-sarah-
c-watkinson/
ebookgate.com
. .C/C++ Programmer's Reference, Third Edition
by Herbert Schildt ISBN:0072227222
This guide reviews the rules and syntax of the principle commands that comprise C and its
object-oriented cousin, C++. The reference consists of 19 chapters that define operators and
the standard input/output, string, character, and more.
Table of Contents
Quickly find the syntax for keywords, operators, functions, classes, and preprocessor directives
Fully covers the Standard Template Library (STL) and the I/O system
Packed with programming tips to speed your work
Solve problems in all C/C++ environments—including Visual C++
Herbert Schildt, the world’s leading programming author, is an authority on the C, C++, Java, and C# programming
languages and a master Windows programmer. He was a member of the ANSI/ISO committees that standardized C and
C++.
C/C++ Programmer’s Reference, Third Edition
Herbert Schildt
McGraw-Hill/Osborne
McGraw-Hill/Osborne
2600 Tenth Street
Berkeley, California 94710
U.S.A.
To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact
McGraw-Hill/Osborne at the above address. For information on translations or book distributors outside the U.S.A.,
please see the International Contact Information page immediately following the index of this book.
Copyright © 2003 by The McGraw-Hill Companies. All rights reserved. Printed in the United States of America. Except
as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form
or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with the
exception that the program listings may be entered, stored, and executed in a computer system, but they may not be
reproduced for publication.
ISBN 0-07-222722-2
Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable. However, because of the
possibility of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne does
not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or
omissions or the results obtained from the use of such information.
About the Author
Herbert Schildt is a leading authority on C and C++ and was a member of the ANSI/ISO committees that standardized
C and C++. His books have sold more than 3 million copies worldwide and have been translated into all major foreign
languages. He is the author of C: The Complete Reference, C++: The Complete Reference, C++: A Beginner’s Guide,
C++ from the Ground Up, and many other best-sellers, including C#: The Complete Reference and Java 2: The
Complete Reference. Schildt holds a Master’s degree in computer science from the University of Illinois.
Introduction
C and C++ are two of the world’s most important programming languages. Indeed, to be a professional programmer
today implies proficiency in these two languages. They are the foundation upon which modern programming is built.
C was invented by Dennis Ritchie in the 1970s. C is a middle-level language. It combines the control structures of a
high-level language with the ability to manipulate bits, bytes, and pointers (addresses). Thus, C gives the programmer
nearly complete control over the machine. C was first standardized late in 1989 when the American National
Standards Institute (ANSI) standard for C was adopted. This version of C is commonly referred to as C89. This
standard was also adopted by ISO (International Standards Organization). C89 was amended slightly in 1995.
C++ was created by Bjarne Stroustrup, beginning in 1979. The development and refinement of C++ was a major effort,
spanning the 1980s and most of the 1990s. Finally, in 1998 an ANSI/ISO standard for C++ was adopted. In general
terms, C++ is the object-oriented version of C. C++ is built upon the foundation of C89, including its 1995
amendments. In fact, the version of C defined by C89 is commonly referred to as the “C subset of C++.” Although C++
began as a set of object-oriented extensions to C, it soon expanded into being a programming language in its own
right. Today, C++ is nearly twice the size of the C language. Needless to say, C++ is one of the most powerful
computer languages ever devised.
In 1999, a new ANSI/ISO standard for C was adopted. This version is called C99. It includes a number of refinements
and several new features. Some of these ?new?features were borrowed from C++, but some are entirely new
innovations. Thus, several of the elements added by C99 are incompatible with C++. This means that with the advent
of C99, Standard C is no longer a pure subset of C++. Fortunately, many of the incompatibilities relate to special-use
features that are readily avoided. Thus, it is still easy to write code that is compatible with both C and C++. At the time
of this writing, no major compiler currently accepts all of the C99 additions.
The following table synopsizes the relationships between C89, C99, and C++.
C89 The original ANSI/ISO standard for C. C89 is what most programmers today think of as C.
C++ The object-oriented version of C. The current ANSI/ISO standard for C++ is built upon C89. Thus, C89
forms a subset of C++.
C99 The latest standard for C. Includes all of C89, but adds several new features. Some of the new
features are not supported by the current standard for C++.
The material in this book describes C89, C99, and C++. When a feature is unique to one of these, it will be so flagged.
Otherwise, you can assume that the feature applies to all three.
As you are undoubtedly aware, C and C++ are large topics. It is, of course, not possible to cover every aspect of these
important languages here. Instead, this quick reference distills their most salient features into a convenient and easy to
use form.
Chapter 1: Data Types, Variables, and Constants
C and C++ offer the programmer a rich assortment of built-in data types. Programmer-defined data types can be
created to fit virtually any need. Variables can be created for any valid data type. Also, it is possible to specify
constants of C/C++’s built-in types. In this section, various features relating to data types, variables, and constants are
discussed.
Type Keyword
Character char
Integer int
Valueless void
Type Keyword
Complex _Complex
Imaginary _Imaginary
Type Keyword
Character char
Integer int
Valueless void
As you can see, all versions of C and C++ provide the following five basic types: char, int, float, double, and void.
Also notice that the keyword for the Boolean type is bool in C++ and _Bool in C99. No Boolean type is included in C89.
Several of the basic types can be modified using one or more of these type modifiers:
signed
unsigned
short
long
The type modifiers precede the type name that they modify. The basic arithmetic types, including modifiers, allowed by
C and C++ are shown in the following table along with their guaranteed minimum ranges. Most compilers will exceed
the minimums for one or more types. Also, if your computer uses two’s complement arithmetic (as most do), then the
smallest negative value that can be stored by a signed integer will be one more than the minimums shown. For
example, the range of an int for most computers is –32,768 to 32,767. Whether type char is signed or unsigned is
implementation dependent.
signed long long int same as long long int (C99 only)
When a type modifier is used by itself, int is assumed. For example, you can specify an unsigned integer by simply
using the keyword unsigned. Thus, these declarations are equivalent.
unsigned int i; // here, int is specified
unsigned i; // here, int is implied
Declaring Variables
All variables must be declared prior to use. Here is the general form of a declaration:
type variable_name;
For example, to declare x to be a float, y to be an integer, and ch to be a character, you would write
float x;
int y;
char ch;
You can declare more than one variable of a type by using a comma-separated list. For example, the following
statement declares three integers:
int a, b, c;
Initializing Variables
A variable can be initialized by following its name with an equal sign and an initial value. For example, this declaration
assigns count an initial value of 100:
int count = 100;
An initializer can be any expression that is valid when the variable is declared. This includes other variables and
function calls. However, in C, global variables and static local variables must be initialized using only constant
expressions.
Identifiers
Variable, function, and user-defined type names are all examples of identifiers. In C/C++, identifiers are sequences of
letters, digits, and underscores from one to several characters in length. (A digit cannot begin a name, however.)
Identifiers may be of any length. However, not all characters will necessarily be significant. There are two types of
identifiers: external and internal. An external identifier will be involved in an external link process. These identifiers,
called external names, include function names and global variable names that are shared between files. If the identifier
is not used in an external link process, it is internal. This type of identifier is called an internal name and includes the
names of local variables, for example. In C89, at least the first 6 characters of an external identifier and at least the
first 31 characters of an internal identifier will be significant. C99 has increased these values. In C99, an external
identifier has at least 31 significant characters and an internal identifier has at least 63 significant characters. In C++, at
least the first 1,024 characters of an identifier are significant.
The underscore is often used for clarity, such as first_time, or to begin a name, such as _count. Uppercase and
lowercase are different. For example, test and TEST are two different variables. C/C++ reserves all identifiers that
begin with two underscores, or an underscore followed by an uppercase letter.
Classes
The class is C++’s basic unit of encapsulation. A class is defined using the class keyword. Classes are not part of the
C language. A class is essentially a collection of variables and functions that manipulate those variables. The variables
and functions that form a class are called members. The general form of class is shown here:
Here, class-name is the name of the class type. Once the class declaration has been compiled, the class-name
becomes a new type name that can be used to declare objects of the class. The object-list is a comma-separated list
of objects of type class-name. This list is optional. Class objects can be declared later in your program by simply using
the class name. The inheritance-list is also optional. When present, it specifies the base class or classes that the new
class inherits. (See the following section entitled ?Inheritance.?)
A class can include a constructor function and a destructor function. (Either or both are optional.) A constructor is
called when an object of the class is first created. The destructor is called when an object is destroyed. A constructor
has the same name as the class. A destructor has the same name as the class, but is preceded by a ~ (tilde). Neither
constructors nor destructors have return types. In a class hierarchy, constructors are executed in order of derivation
and destructors are executed in reverse order.
By default, all elements of a class are private to that class and can be accessed only by other members of that class.
To allow an element of the class to be accessed by functions that are not members of the class, you must declare
them after the keyword public. For example:
class myclass {
int a, b; // private to myclass
public:
// class members accessible by nonmembers
void setab(int i, int j) { a = i; b = j; }
void showab() { cout << a << ' ' << b << endl; }
};
This declaration creates a class type, called myclass, that contains two private variables, a and b. It also contains two
public functions called setab( ) and showab( ). The fragment also declares two objects of type myclass called ob1 and
ob2.
To allow a member of a class to be inherited, but to otherwise be private, specify it as protected. A protected member
is available to derived classes, but is unavailable outside its class hierarchy.
When operating on an object of a class, use the dot (.) operator to reference individual members. The arrow operator
(–>) is used when accessing an object through a pointer. For example, the following accesses the putinfo( ) function of
ob using the dot operator and the show( ) function using the arrow operator:
struct cl_type {
int x;
float f;
public:
void putinfo(int a, float t) { x = a; f = t; }
void show() { cout << a << ' ' << f << endl; }
};
ob.putinfo(10, 0.23);
It is possible to create generic classes by using the template keyword. (See template in the keyword summary in
Chapter 5.)
Inheritance
In C++, one class can inherit the characteristics of another. The inherited class is usually called the base class. The
inheriting class is referred to as a derived class. When one class inherits another, a class hierarchy is formed. The
general form for inheriting a class is
Here, access determines how the base class is inherited, and it must be either private, public, or protected. (It can also
be omitted, in which case public is assumed if the derived class is a struct, or private if the derived class is a class.) To
inherit more than one class, use a comma-separated list.
If access is public, all public and protected members of the base class become public and protected members of the
derived class, respectively. If access is private, all public and protected members of the base class become private
members of the derived class. If access is protected, all public and protected members of the base class become
protected members of the derived class.
In the following class hierarchy, derived inherits base as private. This means that i becomes a private member of
derived.
class base {
public:
int i;
};
In C, several restrictions apply to structures. First, they may contain only data members; member functions are not
allowed. C structures do not support inheritance. Also, all members are public and the keywords public, protected, and
private are not allowed.
Unions
A union is a class type in which all data members share the same memory location. In C++, a union may include both
member functions and data. In
a union, all of its members are public by default. To create private elements, you must use the private keyword. The
general form for declaration of
a union is
union class-name {
// public members by default
private:
// private members
} object-list;
In C, unions may contain only data members and the private keyword is not supported.
declares union tom , which looks like this in memory (assuming 2-byte integers):
Like a class, the individual variables that comprise the union are referenced using the dot operator. The arrow operator
is used with a pointer to a union.
There are several restrictions that apply to unions. First, a union cannot inherit any other class of any type. A union
cannot be a base class. A union cannot have virtual member functions. No members may be declared as static. A
reference member cannot be used. A union cannot have as a member any object that overloads the = operator.
Finally, no object can be a member of a union if the object’s class explicitly defines a constructor or destructor
function. (Objects that have only the default constructors and destructors are acceptable.)
Programming Tip In C++, it is common practice to use struct when creating C-style structures that
include only data members. A class is usually reserved for creating classes that
contain function members. Sometimes the acronym POD is used to describe a
C-style structure. POD stands for Plain Old Data.
There is a special type of union in C++ called an anonymous union. An anonymous union declaration does not contain
a class name and no objects of that union are declared. Instead, an anonymous union simply tells the compiler that its
member variables are to share the same memory location. However, the variables themselves are referred to directly,
without using the normal dot or arrow operator syntax. The variables that make up an anonymous union are at the
same scope level as any other variable declared within the same block. This implies that the union variable names
must not conflict with any other names valid within their scope. For example, here is an anonymous union:
union { // anonymous union
int a; // a and f share
float f; // the same memory location
};
// ...
a = 10; // access a
cout << f; // access f
Here, a and f both share the same memory location. As you can see, the names of the union variables are referred to
directly without the use of the dot or arrow operator.
All restrictions that apply to unions in general apply to anonymous unions. In addition, anonymous unions must contain
only data—no member functions are allowed. Anonymous unions may not contain the private or protected keywords.
Finally, an anonymous union with namespace scope must be declared as static.
Enumerations
Another type of variable that can be created is called an enumeration. An enumeration is a list of named integer
constants. Thus, an enumeration type is simply a specification of the list of names that belong to the enumeration.
To create an enumeration requires the use of the keyword enum. The general form of an enumeration type is
The enum-name is the enumeration’s type name. The list of names is comma separated.
For example, the following fragment defines an enumeration of cities called cities and the variable c of type cities.
Finally, c is assigned the value “Houston”.
enum cities { Houston, Austin, Amarillo} c;
c = Houston;
In an enumeration, the value of the first (leftmost) name is, by default, 0; the second name has the value 1; the third
has the value 2; and so on. In general, each name is given a value one greater than the name that precedes it. You
can give a name a specific value by adding an initializer. For example, in the following enumeration, Austin will have
the value 10:
enum cities { Houston, Austin=10, Amarillo };
In this example, Amarillo will have the value 11 because each name will be one greater than the one that precedes it.
C Tags
In C, the name of a structure, union, or enumeration does not define a complete type name. In C++, it does. For
example, the following fragment is valid for C++, but not for C:
struct s_type { int i;
double d;
};
// ...
s_type x; // OK for C++, but not for C
In C++, s_type defines a complete type name and can be used, by itself, to declare objects. In C, s_type defines a tag,
which is not a complete type specifier. In C, you need to precede a tag name with either struct, union, or enum when
declaring objects. For example,
struct s_type x; // now OK for C
extern
If the extern modifier is placed before a variable name, the compiler will know that the variable has external linkage.
External linkage means that an object is visible outside its own file. In essence, extern tells the compiler the type of a
variable without actually allocating storage for it. The extern modifier is most commonly used when there are two or
more files sharing the same global variables.
auto
auto tells the compiler that the local variable it precedes is created upon entry into a block and destroyed upon exit
from a block. Since all variables defined inside a function are auto by default, the auto keyword is seldom (if ever)
used.
register
When C was first invented, the register modifier could be used only on local integer, character, or pointer variables
because it caused the compiler to attempt to keep that variable in a register of the CPU instead of placing it in
memory. This made all references to that variable extremely fast. The definition of register has since been expanded.
Now, any variable may be specified as register and it is the compiler’s job to optimize accesses to it. For characters,
integers, and pointers, this still means putting them into a register in the CPU, but for other types of data, it may mean
using cache memory, for example. Keep in mind that register is only a request. The compiler is free to ignore it. The
reason for this is that only so many variables can be optimized for speed. When this limit is exceeded, the compiler will
simply ignore further register requests.
static
The static modifier instructs the compiler to keep a local variable in existence during the lifetime of the program
instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables
static allows them to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be
restricted to the file in which it is declared. This means that it will have internal linkage. Internal linkage means that an
identifier is known only within its own file.
In C++, when static is used on a class data member, it causes only one copy of that member to be shared by all
objects of its class.
mutable
The mutable specifier applies to C++ only. It allows a member of an object to override constness. That is, a mutable
member can be modified by a const member function.
Type Qualifiers
The type qualifiers provide additional information about the variables they precede.
const
Objects of type const cannot be changed by your program during execution. Also, an object pointed to by a const
pointer cannot be modified. The compiler is free to place variables of this type into read-only memory (ROM). A const
variable will receive its value either from an explicit initialization or by some hardware-dependent means. For example,
const int a = 10;
will create an integer called a with a value of 10 that may not be modified by your program. It can, however, be used in
other types of expressions.
volatile
The modifier volatile tells the compiler that a variable’s value may be changed in ways not explicitly specified by the
program. For example, a global variable’s address may be passed to the clock routine of the operating system and
updated with each clock tick. In this situation, the contents of the variable are altered without any explicit assignment
statements in the program. This is important because compilers will sometimes automatically optimize certain
expressions by making the assumption that the contents of a variable are unchanging inside an expression. This is
done to achieve higher performance. The volatile modifier will prevent this optimization in those rare situations where
this assumption is not the case.
Programming Tip If a class member function is modified by const, it cannot alter the object that invokes
the function. To declare a const member function, put const after its parameter list.
For example,
class MyClass {
int i;
public:
// a const function
void f1(int a) const {
i = a; // Error! can't modify invoking object
}
void f2(int a) {
i = a; // OK, not const function
}
};
As the comments suggest, f1( ) is a const function and it cannot modify the object that
invokes it.
restrict
C99 adds a new type qualifier called restrict. This qualifier applies only to pointers. A pointer qualified by restrict is
initially the only means by which the object it points to can be accessed. Access to the object by another pointer can
occur only if the second pointer is based on the first. Thus, access to the object is restricted to expressions based on
the restrict-qualified pointer. Pointers qualified by restrict are primarily used as function parameters, or to point to
memory allocated via malloc( ) . The restrict qualifier does not change the semantics of a program. restrict is not
supported by C++.
Arrays
You may declare arrays of any data type, including classes. The general form of a singly dimensioned array is
type var-name[size];
where type specifies the data type of each element in the array and size specifies the number of elements in the array.
For example, to declare an integer array x of 100 elements, you would write
int x[100];
This will create an array that is 100 elements long with the first element being 0 and the last being 99. For example,
the following loop will load the numbers 0 through 99 into array x:
for(t=0; t<100; t++) x[t] = t;
Multidimensional arrays are declared by placing the additional dimensions inside additional brackets. For example, to
declare a 10 × 20 integer array, you would write
int x[10][20];
In C89 and C++, array dimensions must be specified by constant values. Thus, in C89 and C++, all array dimensions
are fixed at compile time and cannot change over the lifetime of a program. However, in C99, the dimensions of a local
array can be specified by any valid integer expression, including those whose values are known only at compile time.
This is called a variable- length array. Thus, the dimensions of a variable-length array can differ each time its
declaration statement is encountered.
Defining New Type Names Using typedef
You can create a new name for an existing type using typedef. Its general form is
For example, the following tells the compiler that feet is another name for int:
typedef int feet;
Now, the following declaration is perfectly legal and creates an integer variable called distance:
feet distance;
Constants
Constants, also called literals, refer to fixed values that cannot be altered by the program. Constants can be of any of
the basic data types. The way each constant is represented depends upon its type. Character constants are enclosed
between single quotes. For example 'a' and '+' are both character constants. Integer constants are specified as
numbers without fractional components. For example, 10 and –100 are integer constants. Floating-point constants
require the use of the decimal point followed by the number’s fractional component. For example, 11.123 is a
floating-point constant. You may also use scientific notation for floating-point numbers.
There are two floating-point types: float and double. Also, there are several flavors of the basic types that are
generated using the type modifiers. By default, the compiler fits a numeric constant into the smallest compatible data
type that will hold it. The only exceptions to the smallest-type rule are floating-point constants, which are assumed to
be of type double. For many programs, the compiler defaults are perfectly adequate. However, it is possible to specify
precisely the type of constant you want.
To specify the exact type of numeric constant, use a suffix. For floating-point types, if you follow the number with an F,
the number is treated as a float. If you follow it with an L, the number becomes a long double. For integer types, the U
suffix stands for unsigned and the L for long. Some examples are shown next:
C99 also allows you to specify a long long integer constant by specifying the suffix LL (or ll).
It is sometimes easier to use a number system based on 8 or 16 instead of 10. The number system based on 8 is
called octal and uses the digits 0 through 7. In octal, the number 10 is the same as 8 in decimal. The base 16 number
system is called hexadecimal and uses the digits 0 through 9 plus the letters A through F, which stand for 10, 11, 12,
13, 14, and 15. For example, the hexadecimal number 10 is 16 in decimal. Because of the frequency with which these
two number systems are used, C/C++ allows you to specify integer constants in hexadecimal or octal instead of
decimal if you prefer. A hexadecimal constant must begin with a 0x (a zero followed by an x) or 0X, followed by the
constant in hexadecimal form. An octal constant begins with a zero. Here are two examples:
int hex = 0x80; // 128 in decimal
int oct = 012; // 10 in decimal
String Constants
C/C++ supports one other type of constant in addition to those of the predefined data types: a string. A string is a set of
characters enclosed by double quotes. For example, "this is a test" is a string. You must not confuse strings with
characters. A single-character constant is enclosed by single quotes, such as 'a'. However, "a" is a string containing
only one letter. String constants are automatically null terminated by the compiler. C++ also supports a string class,
which is described later in this book.
Boolean Constants
C++ specifies two Boolean constants: true and false.
C99, which adds the _Bool type to C, does not specify any built-in Boolean constants. However, if your program
includes the header <stdbool.h>, then the macros true and false are defined. Also, including <stdbool.h> causes the
macro bool to be defined as another name for _Bool. Thus, it is possible to create code that is compatible with both
C99 and C++. Remember, however, that C89 does not define a Boolean type.
Complex Constants
In C99, if you include the header <complex.h>, then the following complex and imaginary constants are defined.
Here, i represents the imaginary value, which is the square root of –1.
Enclosing character constants in single quotes works for most printing characters, but a few, such as the carriage
return, are impossible to enter into your program’s source code from the keyboard. For this reason, C/C++ recognizes
several backslash character constants, also called escape sequences. These constants are listed here:
Code Meaning
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\\ Backslash
\v Vertical tab
\a Alert
\? Question mark
The backslash constants can be used anywhere a character can. For example, the following statement outputs a
newline and a tab and then prints the string “This is a test”.
cout << "\n\tThis is a test";
Chapter 2: Functions, Scopes, Namespaces, and Headers
Functions are the building blocks of a C/C++ program. The elements of a program, including functions, exist within
one or more scopes. In C++, there is a special scope called a namespace. The prototypes for all standard functions
are declared within various headers. These topics are examined here.
Functions
At the heart of a C/C++ program is the function. It is the place in which all program activity occurs. The general form of
a function is
The type of data returned by a function is specified by ret-type. The parameter list is a comma-separated list of
variables that will receive any arguments passed to the function. For example, the following function has two integer
parameters called i and j and a double parameter called count:
void f(int i, int j, double count)
{ ...
In C89, if a function’s return type is not explicitly specified, it defaults to int. C++ and C99 do not support
“default-to-int,” although most compilers will still allow it.
Functions terminate and return automatically to the calling procedure when the last brace is encountered. You may
force a return prior to that by using the return statement.
All functions, except those declared as void, return a value. The type of the return value must match the type
declaration of the function. Values are returned via the return statement.
In C++, it is possible to create generic functions using the keyword template. (See template in Chapter 5.)
Recursion
In C/C++, functions can call themselves. This is called recursion, and a function that calls itself is said to be recursive.
A simple example is the function factr( ) shown here, which computes the factorial of an integer. The factorial of a
number N is the product of all the whole numbers from 1 to N. For example, 3 factorial is 1 × 2 × 3, or 6.
// Compute the factorial of a number using recursion.
int factr(int n)
{
int answer;
if(n==1) return 1;
answer = factr(n-1)*n;
return answer;
}
When factr( ) is called with an argument of 1, the function returns 1; otherwise, it returns the product of factr(n–1) * n.
To evaluate this expression, factr( ) is called with n–1 . This process continues until n equals 1 and the calls to the
function begin returning. When factr( ) finally returns to the original caller, the final return value will be the factorial of
the original argument.
Programming Tip Although powerful, recursive functions should be used with care. The recursive
versions of many routines execute a bit more slowly than the iterative equivalent
because of the added overhead of the repeated function calls. Many recursive calls to
a function could cause a stack overrun. Because storage for function parameters and
local variables is on the stack and each new call creates a new copy of these
variables, the stack space could become exhausted. If this happens, a stack overflow
occurs. If this happens in the normal use of a debugged recursive function, try
increasing the stack space allocated to your program.
When a function calls itself, new local variables and parameters are allocated storage on the stack, and the function
code is executed with these new variables from its beginning. A recursive call does not make a new copy of the
function. Only the arguments and local variables are new. As each recursive call returns, the old local variables and
parameters are removed from the stack and execution resumes at the point of the recursive call inside the function.
Recursive functions could be said to “telescope” out and back.
Function Overloading
In C++, functions can be overloaded. When a function is overloaded,two or more functions share the same name.
However, each version of an overloaded function must have a different number and/or type of parameters. (The
function return types may also differ, but this is not necessary.) When an overloaded function is called, the compiler
decides which version of the function to use based upon the type and/or number of arguments, calling the function that
has the closest match. For example, given these three overloaded functions,
void myfunc(int a) { cout << "a is " << a << endl;
}
// overload myfunc
void myfunc(int a, int b) {
cout << "a is " << a << endl;
cout << "b is " << b << endl;
}
In each case, the type and number of arguments determine which version of myfunc( ) is actually executed.
When you create functions that have default arguments, you must specify the default values only once: either in the
function prototype or in its definition. (You cannot specify them each place, even if you use the same values.)
Generally, default values are specified in the prototype.
When giving a function default arguments, remember that you must specify all nondefaulting arguments first. Once
you begin to specify default arguments, there may be no intervening nondefaulting ones.
In essence, a prototype is simply the return type, name, and parameter list of a function, followed by a semicolon.
.
.
.
// function definition
float fn(float x)
{
// ...
}
To specify the prototype for a function that takes a variable number of arguments, use three periods at the point at
which the variable number of parameters begin. For example, the printf( ) function could be prototyped like this:
int printf(const char *format, ...);
When specifying the prototype to an overloaded function, each version of that function must have its own prototype.
When a member function is declared within its class, this constitutes a prototype for the function.
In C, to specify the prototype for a function that has no parameters, use void in its parameter list. For example,
int f(void);
In C++, an empty parameter list in a prototype means that the function has no parameters; the void is optional. Thus,
in C++, the preceding prototype can be written like this:
int f();
In C++, you can include void in the parameter list, but doing so is redundant.
Programming Tip Two terms are commonly confused in C/C++ programming: declaration and definition.
Here is what they mean. A declaration specifies the name and type of an object. A
definition allocates storage for it. These definitions apply to functions, too. A function
declaration (prototype) specifies the return type, name, and parameters of a function.
The function itself (that is, the function with its body) is its definition.
The global scope exists outside all other scopes. A name declared in the global scope is known throughout the
program. For example, a global variable is available for use by all functions in the program. Global variables stay in
existence the entire duration of the program.
A local scope is defined by a block. That is, a local scope is begun by an opening brace and ends with its closing
brace. A name declared within a local scope is known only within that scope. Because blocks can be nested, local
scopes, too, can be nested. Of course, the most common local scope is the one defined by a function. Local variables
are created when their block is entered, and destroyed when their block is exited. This means that local variables do
not hold their values between function calls. You can use the static modifier, however, to preserve values between
calls.
In C++ and C99, local variables can be declared nearly anywhere within a block. In C89, they must be declared at the
start of a block, before any “action” statements occur. For example, the following code is valid for C++ and C99, but
not for C89:
void f(int a)
{
int a;
a = 10;
A global variable must be declared outside of all functions, including outside the main( ) function. Global variables are
generally placed at the top of the file, prior to main( ) , for ease of reading and because a variable must be declared
before it is used.
The formal parameters to a function are also local variables and, aside from their job of receiving the value of the
calling arguments, behave and can be used like any other local variable.
Namespaces
In C++, it is possible to create a local scope using the namespace keyword. A namespace defines a declarative
region. Its purpose is to localize names. The general form of namespace is shown here:
namespace name {
// ...
}
This creates a namespace called MyNameSpace and the variable count is declared inside it.
Names declared within a namespace can be referred to directly by other statements within the same namespace.
Outside their namespace, names can be accessed two ways. First, you can use the scope resolution operator. For
example, assuming MyNameSpace just shown, the following statement is valid:
MyNameSpace::count = 10;
You can also specify a using statement, which brings the specified name or namespace into the current scope. For
example,
using namespace MyNameSpace;
count = 100;
In this case, count can be referred to directly because it has been brought into the current scope.
When C++ was originally invented, items declared in the C++ library were in the global (i.e., unnamed) namespace.
However, Standard C++ puts all of these items into the std namespace.
The main( ) Function
In a C/C++ program, execution begins at main( ) . (Windows programs call WinMain( ), but this is a special case.) You
must not have more than one function called main( ) . When main( ) terminates, the program is over and control passes
back to the operating system.
The main( ) function is not prototyped. Thus, different forms of main( ) may be used. For both C and C++, the following
versions of main( ) are valid:
int main()
int main(int argc, char *argv[])
As the second form shows, at least two parameters are supported by main( ) . They are argc and argv. (Some
compilers will allow additional parameters.) These two variables will hold the number of command-line arguments and
a pointer to them, respectively. argc is an integer, and its value will always be at least 1 because the program name is
the first argument as far as C/C++ is concerned. argv must be declared as an array of character pointers. Each pointer
points to a command-line argument. Their usage is shown below in a short program that will print your name on the
screen:
#include <iostream>
using namespace std;
return 0;
}
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables
are called the formal parameters of the function. They behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit. As with local variables, you can make assignments to a
function’s formal parameters or use them in any allowable C/C++ expression. Even though these variables perform the
special task of receiving the value of the arguments passed to the function, they can be used like any other local
variable.
In general, subroutines can be passed arguments in one of two ways. The first is called call by value. This method
copies the value of an argument into the formal parameter of the subroutine. Changes made to the parameters of the
subroutine have no effect on the variables used to call it. Call by reference is the second way a subroutine can have
arguments passed to it. In this method, the address of an argument is copied into the parameter. Inside the subroutine,
the argument is accessed through this address. This means that changes made to the parameter affect the variable
used to call the routine.
By default, C and C++ use call by value to pass arguments. This means that you generally cannot alter the variables
used to call the function. Consider the following function:
int sqr(int x)
{
x = x*x;
return x;
}
In this example, when the assignment x = x * x takes place, the only thing modified is the local variable x. The
argument used to call sqr( ) still has its original value.
Remember that only a copy of the value of the argument is passed to a function. What occurs inside the function has
no effect on the variable used in the call.
Passing Pointers
Even though C and C++ use call-by-value parameter passing by default, it is possible to manually construct a call by
reference by passing a pointer to the argument. Since this passes the address of the argument to the function, it is
then possible to change the value of the argument outside the function.
Pointers are passed to functions just like any other value. Of course, it is necessary to declare the parameters as
pointer types. For example, the function swap( ), which exchanges the value of its two integer arguments, is shown
here:
// Use pointer parameters.
void swap(int *x, int *y)
{
int temp;
It is important to remember that swap( ) (or any other function that uses pointer parameters) must be called with the
addresses of the arguments. The following fragment shows the correct way to call swap( ):
int a, b;
a = 10;
b = 20;
swap(&a, &b);
In this example, swap( ) is called with the addresses of a and b. The unary operator & is used to produce the
addresses of the variables. Therefore, the addresses of a and b, not their values, are passed to the function swap( ).
After the call, a will have the value 20 and b will have the value 10.
Reference Parameters
In C++, it is possible to automatically pass the address of a variable to a function. This is accomplished using a
reference parameter. When using a reference parameter, the address of an argument is passed to the function and
the function operates on the argument, not a copy.
To create a reference parameter, precede its name with the & (ampersand). Inside the function, you can use the
parameter normally, without any need to use the the * (asterisk) operator. The compiler will automatically dereference
the address for you. For example, the following creates a version of swap( ) that uses two reference parameters to
exchange the values of its two arguments:
// Use reference parameters.
void swap(int &x, int &y)
{
int temp;
When invoking swap( ), you simply use the normal function-call syntax. For example,
int a, b;
a = 10;
b = 20;
swap(a, b);
Because x and y are now reference parameters, the addresses of a and b are automatically generated and passed to
the function. Inside the function, the parameter names are used without any need for the * operator because the
compiler automatically refers to the calling arguments each time x and y are used.
Constructors may have parameters. You can use these parameters to pass values to a constructor, which can be
used to initialize an object. The arguments that are passed to the parameters are specified when an object is created.
For example, this fragment illustrates how to pass a constructor an argument:
class myclass {
int a;
public:
myclass(int i) { a = i; } // constructor
~myclass() { cout << "Destructing..."; }
};
// ...
When ob is declared, the value 3 is passed to the constructor’s parameter i, which is then assigned to a.
In an inheritance hierarchy, a base class’ constructor is automatically called before the derived class’ constructor. If
you need to pass arguments to the base class constructor, you do so by using an expanded form of the derived class’
constructor declaration. Its general form is shown here:
derived-constructor(arg-list) : base1(arg-list),
base2(arg-list),
// ...
baseN(arg-list)
{
// body of derived constructor
}
Here, base1 through baseN are the names of the base classes inherited by the derived class. Notice that a colon
separates the derived class’ constructor declaration from the base class specifications, and that the base class
specifications are separated from each other by commas, in the case of multiple base classes. Here is an example:
class base {
protected:
int i;
public:
base(int x) { i = x; }
// ...
};
void show() { cout << i << " " << j << "\n"; }
// ...
};
Here, derived’s constructor is declared as taking two parameters, x and y. However, derived( ) uses only x; y is passed
along to base( ) . In general, the derived class’ constructor must declare the parameter(s) that it requires as well as any
required by the base class. As the example illustrates, any values required by the base class are passed to it in the
base class’ argument list specified after the colon.
Function Specifiers
C++ defines three function specifiers: inline, virtual, and explicit. The inline specifier is also supported by C99. inline is
a request to the compiler to expand a function’s code inline rather than to call it. If the compiler cannot inline the
function, it is free to ignore the request. Both member and nonmember functions may be specified as inline.
Programming Tip There are two ways a constructor can initialize member variables. First, it can
explicitly assign them values inside the body of the constructor. For example,
consider MyClass, shown here, which has two integer data members callednumA and
numB . These member variables are initialized inside theMyClass constructor.
class MyClass {
int numA;
int numB;
public:
/* Initialize numA and numB inside the MyClass
constructor using normal syntax. */
MyClass(int x, int y) {
numA = x;
numB = y;
}
// ...
};
Assigning initial values to member variables numA and numB inside the constructor,
as MyClass does, is a common approach, and is the way that member initialization is
accomplished for many classes. However, this approach won't work in all cases. For
example, if numA and numB were specified as const, like this,
class MyClass {
const int numA; // const member
const int numB; // const member
then they could not be given values by the MyClass constructor because const
variables must be initialized and cannot be assigned values after the fact. Similar
problems arise when using reference members, which must be initialized, and when
using class members that don't have default constructors. To solve these types of
problems, C++ supports a second member initialization syntax, which gives a class
member an initial value when an object of the class is created.
The member initialization syntax is similar to that used to call a base class
constructor. Here is the general form:
constructor(arg-list) : member1(initializer),
member2(initializer),
// ...
memberN(initializer)
{
// body of constructor
}
The members that you want to initialize are specified in a comma- separated list that
goes after the constructor's declaration and before the body of the constructor. Notice
the use and placement of the colon. You can mix calls to base class constructors with
member initializations in the same list.
Here is MyClass rewritten so thatnumA and numB are const members that are given
values using the member initialization syntax.
class MyClass {
const int numA; // const member
const int numB; // const member
public:
// Initialize using initialization syntax.
MyClass(int x, int y) : numA(x), numB(y) { }
// ...
};
Here, numA is initialized with the value passed inx, and numB is initialized with the
value passed in y. Even though numA and numB are now const, they can be given
initial values when a MyClass object is created because the member initialization
syntax is used.
One last point: Class members are constructed and initialized in the order in which
they are declared in a class, not in the order in which their initializers occur.
A virtual function is defined in a base class and overridden by a derived class. Virtual functions are how C++ supports
polymorphism.
The explicit specifier applies only to constructors. Any time that you have a constructor that requires only one
argument, you can use either ob(x) or ob = x to initialize an object. The reason for this is that whenever you create a
constructor that takes one argument, you are also implicitly creating a conversion from the type of that argument to the
type of the class. A constructor specified as explicit will be used only when an initialization uses the normal constructor
syntax, ob(x). No automatic conversion will take place and ob = x will not be allowed. Thus, an explicit constructor
creates a “nonconverting constructor.”
Linkage Specification
Because it is common to link a C++ function with functions generated by another language (such as C), C++ allows
you to specify a linkage specification that tells the compiler how to link a function. It has this general form:
As you can see, the linkage specification is an extension to the extern keyword. Here, language denotes the language
to which you want the function to link. C and C++ linkages are guaranteed to be supported. Your compiler may support
other linkages, too. To declare several functions using the same linkage specification, you can use this general form:
extern"language" {
function-prototypes
}
Before your program can use a library function, it must include the appropriate header. In general, headers are usually
files, but they are not necessarily files. It is permissible for a compiler to predefine the contents of a header internally.
However, for all practical purposes, the standard C headers are contained in files that correspond to their names. The
following table shows the standard headers defined by C89, along with those added by the 1995 Amendment 1.
Header Supports
<iso646.h> Macros that correspond to various operators, such as && and ^; added in 1995 by
Amendment 1
<locale.h> Localization
Header Supports
<fenv.h> The floating-point status flags and other aspects of the floating-point environment.
<inttypes.h> Standard, portable set of integer type names; also supports functions that handle
greatest-width integers.
Header Supports
<stdbool.h> Boolean data types and defines the macrobool, which helps with C++ compatibility.
<stdint.h> Standard, portable set of integer type names. This file is included byinttypes.h
< >.
For C++, headers are specified using standard header names, which do not end with .h. Thus, the C++ headers do not
specify filenames. Instead, they are simply standard identifiers that the compiler can handle as it sees fit. This means
that a header may be mapped to a filename, but this is not required. The C++ headers are shown here. Those
associated either directly or indirectly with the Standard Template Library (STL) are indicated.
C++ also defines the following headers that correspond to the C headers (except those added by C99).
In standard C++, all of the information relating to the standard library is defined under the std namespace. Thus, to
gain direct access to these items, you will need to include the following using statement after including the necessary
headers:
using namespace std;
Alternatively, you can qualify each library identifier with std::, such as std::cout, rather than bringing the entire library
into the global namespace. However, qualifying each name can get to be tedious.
Programming Tip If you are using an older C++ compiler, then it may not support the modern-style C++
headers or the namespace command. If this is the case, then you will need to use the
older, traditional-style headers. These use the same names as the modern headers
but include the .h (thus, they resemble C headers). For example, the following
includes <iostream> using the traditional approach:
#include <iostream.h>
When using the traditional-style header, all of the names defined by the header are
placed in the global namespace, not the one defined by std. Thus, no using statement
is required.
Chapter 3: Operators
C/C++ has a rich set of operators that can be divided into the following classes: arithmetic, relational and logical,
bitwise, pointer, assignment, I/O, and miscellaneous.
Arithmetic Operators
C/C++ has the following seven arithmetic operators:
Operator Action
+ Addition
* Multiplication
/ Division
% Modulus
–– Decrement
++ Increment
The + , –, *, and / operators work in the expected fashion. The % operator returns the remainder of an integer division.
The increment and decrement operators increase or decrease the operand by one.
Precedence Operators
* / %
Lowest + –
Operator Meaning
== Equal
!= Not equal
Operator Meaning
&& AND
|| OR
! NOT
The relational operators compare two values, producing a Boolean result. The logical operators connect two Boolean
values or, in the case of ! , reverse a value. The precedence of these operators is shown here:
Precedence Operators
Highest !
== !=
&&
Lowest ||
As an example, the following if statement evaluates to true and prints the line x is less than 10:
x = 9;
if(x < 10) cout << "x is less than 10";
However, in the following example, no message is displayed because both operands associated with && must be true
for the outcome to be true:
x = 9;
y = 9;
if(x < 10 && y > 10)
cout << "This will not print.";
The Bitwise Operators
C and C++ provide operators that act upon the actual bits that comprise a value. The bitwise operators can be used
only on integral types. The bitwise operators are as follows:
Operator Meaning
& AND
| OR
^ XOR
~ One’s complement
&, |, and ^
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
These rules are applied to each bit in each operand when the bitwise AND, OR, and XOR operations are performed.
0100 1101
0000 1001
0100 1101
| 0011 1011
0111 1111
0100 1101
^ 0011 1011
0111 0110
The One’s Complement Operator
The one’s complement operator, ~ , will invert all the bits in its operand. For example, if a character variable, ch, has
the bit pattern
0011 1001
then
ch = ~ch;
into ch.
The right (>>) and left (<<) shift operators shift all bits in an integral value by the specified amount. As bits are shifted
off one end, zeros are brought in the other end. (If the value being shifted is a negative, signed number and a right
shift is performed, then ones are shifted in to preserve the sign.) The number on the right side of the shift operator
specifies the number of positions to shift. The general form of each shift operator is
Programming Tip A shift right is effectively a division by 2, and a shift left is a multiplication by 2. For
many computers, a shift is faster than a multiply or a divide. Therefore, if you need a
fast way to multiply or divide by 2, consider using the shift operators. For example,
the following code fragment will first multiply and then divide the value in x by 2:
int x;
x = 10;
x = x << 1;
x = x >> 1;
Of course, when using the shift operators to perform multiplication, you must be
careful not to shift bits off the end.
Precedence Operators
Highest ~
>> <<
&
^
Random documents with unrelated
content Scribd suggests to you:
CHAPTER XXXV
Do not think that what we have laid down in the preceding chapters
on the importance, obscurity, and difficulty of the subject, and its
unsuitableness for communication to ordinary persons, includes the
doctrine of God’s incorporeality and His exemption from all affections
(πάθη). This is not the case. For in the same way as all people must
be informed, and even children must be trained in the belief that
God is One, and that none besides Him is to be worshipped, so must
all be taught by simple authority that God is incorporeal; that there
is no similarity in any way whatsoever between Him and His
creatures; that His existence is not like the existence of His
creatures, His life not like that of any living being, His wisdom not
like the wisdom of the wisest of men; and that the difference
between Him and His creatures is not merely quantitative, but
absolute [as between two individuals of two different classes]; I
mean to say that all must understand that our wisdom and His, or
our power and His do not differ quantitatively or qualitatively, or in a
similar manner; for two things, of which the one is strong and the
other weak, are necessarily similar, belong to the same class, and
can be included in one definition. The same is the case with all other
comparisons; they can only be made between two things belonging
to the same class, as has been shown in works on Natural Science.
Anything predicated of God is totally different from our attributes; no
definition can comprehend both; therefore His existence and that of
any other being totally differ from each other, and the term
existence is applied to both homonymously, as I shall explain.
If a person does not know the measure of the cone, or the sphericity
of the sun, it is not so important as not to know whether God exists,
or whether the world exists without a God; and if a man assumes
that the cone is half (of the cylinder), or that the sun is a circle, it is
not so injurious as to believe that God is more than One. You must
know that idolaters when worshipping idols do not believe that there
is no God besides them; and no idolater ever did assume that any
image made of metal, stone, or wood has created the heavens and
the earth, and still governs them. Idolatry is founded on the idea
that a particular form represents the agent between God and His
creatures. This is plainly said in passages like the following: “Who
would not fear thee, O king of nations?” (Jer. x. 7); “And in every
place incense is offered unto my name” (Mal. i. 11); by “my name”
allusion is made to the Being which is called by them [i.e., the
idolaters] “the First Cause.” We have already explained this in our
larger work (Mishneh Torah, I. On Idolatry, chap. i.), and none of our
co-religionists can doubt it.
[Contents]
CHAPTER XXXVII
The Hebrew term panim (face) is homonymous; most of its various
meanings have a figurative character. It denotes in the first place the
face of a living being; comp. “And all faces are turned into paleness”
(Jer. xxx. 6); “Wherefore are your faces so sad?” (Gen. xl. 7). In this
sense the term occurs frequently.
The next meaning of the word is “anger”; comp. “And her anger
(paneha) was gone” (1 Sam. i. 18). Accordingly, the term is
frequently used in reference to God in the sense of anger and wrath;
comp. “The anger (pene) of the Lord hath divided them” (Lam. iv.
16); “The anger (pene) of the Lord is against them that do evil” (Ps.
xxxiv. 17); “Mine anger (panai) shall go and I will give thee rest”
(Exod. xxxiii. 14); “Then will I set mine anger” (panai) (Lev. xx. 3);
there are many other instances.
[Contents]
CHAPTER XXXVIII
The Hebrew term aḥor is a homonym. It is a noun, signifying “back.”
Comp. “Behind (aḥare) the tabernacle” (Exod. xxvi. 12); “The spear
came out behind him (aḥarav)” (2 Sam. ii. 23). [54]
[Contents]
CHAPTER XXXIX
The Hebrew leb (heart) is a homonymous noun, signifying that
organ which is the source of life to all beings possessing a heart.
Comp. “And thrust them through the heart of Absalom” (1 Sam. xviii.
14).
This organ being in the middle of the body, the word has been
figuratively applied to express “the middle part of a thing.” Comp.
“unto the midst (leb) of heaven” (Deut. iv. 11); “the midst (labbath)
of fire” (Exod. iii. 2).
The word further signifies “counsel.” Comp. “All the rest of Israel
were of one heart (i.e., had one plan) to make David king” (1 Chron.
xii. 38); “but fools die for want of heart,” i.e., of counsel; “My heart
(i.e., my counsel) shall not turn away from this so long as I live” (Job
xxvii. 6); for this sentence is preceded by the words, “My
righteousness I hold fast, and will not let it go”; and then follows,
“my heart shall never turn away from this.”—As regards the
expression yeḥeraf, I think that it may be compared with the same
verb in the form neḥrefet, “a handmaid betrothed (neḥrefet) to a
man” (Lev. xix. 20), where neḥrefetḥ is similar in meaning to the
Arabic munḥarifat, “turning away,” and signifies “turning from the
state of slavery to that of marriage.”
Leb (heart) denotes also “will”; comp. “And I shall give you pastors
according to my will (libbi)” (Jer. iii. 15), “Is thine heart right as my
heart is?” (2 Kings x. 15), i.e., is thy will right as my will is? In this
sense the word has been figuratively applied to God. Comp. “That
shall do according to that which is in mine heart and in my soul” (1
Sam. ii. 35), i.e., according to My will; “And mine eyes and mine
heart (i.e., My providence and My will) shall be there perpetually” (1
Kings ix. 3).
As to the passage, “And thou shalt love the Lord thy God with all
thine heart” (Ib. vi. 5), I explain “with all thine heart” to mean “with
all the powers of thine heart,” that is, with all the powers of the
body, for they all have their origin in the heart; and the sense of the
entire passage is: make the knowledge of God the aim of all thy
actions, as we have stated in our Commentary on the Mishnah
(Aboth, Eight Chapters, v.), and in our Mishneh Torah, yesode
hatorah, chap. ii. 2.
[Contents]
CHAPTER XL
Ruaḥ is a homonym, signifying “air,” that is, one of the four
elements. Comp. “And the air of God moved” (Gen. i. 2).
It denotes also, “wind.” Comp. “And the east wind (ruaḥ) brought
the locusts” (Exod. x. 13); “west wind” (ruaḥ) (ib. 19). In this sense
the word occurs frequently.
It signifies also that which remains of man after his death, and is not
subject to destruction. Comp. “And the spirit (ruaḥ) shall return unto
God who gave it” (Eccles. xii. 7).
[Contents]
CHAPTER XLI
The Hebrew nefesh (soul) is a homonymous noun, signifying the
vitality which is common to all living, sentient beings. E.g. “wherein
there is a living soul” (nefesh) (Gen. i. 30). It denotes also blood, as
in “Thou shalt not eat the blood (nefesh) with the meat” (Deut. xii.
23). Another signification of the term is “reason,” that is, the
distinguishing characteristic of man, as in “As the Lord liveth that
made us this soul” (Jer. xxxviii. 16). It denotes also the part of man
that remains after his death (nefesh, soul); comp. “But the soul
(nefesh) of my lord shall be bound in the bundle of life” (1 Sam. xxv.
29). Lastly, it denotes “will”; comp. “To bind his princes at his will”
(be-nafsho) (Ps. cv. 22); “Thou wilt not deliver me unto the will (be-
nefesh) of my enemies” (Ps. xli. 3); and according to my opinion, it
has this meaning also in the following passages, “If it be your will
(nafshekem) that I should bury my dead” (Gen. xxiii. 8); “Though
Moses and Samuel stood before me, yet my will (nafshi) could not
be toward this people” (Jer. xv. 1), that is, I had no pleasure in
them, I did not wish to preserve them. When nefesh is used in
reference to God, it has the meaning “will,” as we have already
explained with reference to the passage, “That shall do according to
that which is in my will (bi-lebabi) and in mine intention (be-nafshi)”
(1 Sam. ii. 35). Similarly we explain the phrase, “And his will
(nafsho) to trouble Israel ceased” (Judg. x. 16). Jonathan, the son of
Uzziel [in the Targum of the Prophets], did not translate this
passage, because he understood nafshi to have the first
signification, and finding, therefore, in these words sensation
ascribed to God, he omitted them from his translation. If, however,
nefesh be here taken in the last signification, the sentence can well
be explained. For in the passage which precedes, it is stated that
Providence abandoned the Israelites, and left them on the brink of
death; then they cried and prayed for help, but in vain. When,
however, they had thoroughly repented, when their misery had
increased, and their enemy had had power over them, He showed
mercy to them, and His will to continue their trouble and misery
ceased. Note it well, for it is remarkable. The preposition ba in this
passage has the force of the preposition min (“from” or “of”); and
baʻamal is identical with meʻamal. Grammarians give many instances
of this use of the preposition ba: “And that which remaineth of (ba)
the flesh and of (ba) the bread” (Lev. viii. 32); “If there remains but
few of (ba) the years” (ib. xxv. 52); “Of (ba) the strangers and of
(ba) those born in the land” (Exod. xii. 19).
[Contents]
CHAPTER XLII
Ḥai (“living”) signifies a sentient organism (lit. “growing” and “having
sensation”), comp. “Every moving thing that liveth” (Gen. ix. 3); it
also denotes recovery from a severe illness: “And was recovered (va-
yeḥi) of his sickness” (Isa. xxxviii. 9); “In the camp till they
recovered” (ḥayotam) (Josh. v. 8); “quick, raw (ḥai) flesh” (Lev. xiii.
10).
Mavet signifies “death” and “severe illness,” as in “His heart died (va-
yamot) within him, and he became as a stone” (1 Sam. xxv. 37),
that is, his illness was severe. For this reason it is stated concerning
the son of the [57]woman of Zarephath, “And his sickness was so
sore, that there was no breath left in him” (1 Kings xvii. 17). The
simple expression va-yamoth would have given the idea that he was
very ill, near death, like Nabal when he heard what had taken place.
Some of the Andalusian authors say that his breath was suspended,
so that no breathing could be perceived at all, as sometimes an
invalid is seized with a fainting fit or an attack of asphyxia, and it
cannot be discovered whether he is alive or dead; in this condition
the patient may remain a day or two.
The term ḥai has also been employed in reference to the acquisition
of wisdom. Comp. “So shall they be life (ḥayyim) unto thy soul”
(Prov. iii. 22); “For whoso findeth me findeth life” (ib. viii. 35); “For
they are life (ḥayyim) to those that find them” (ib. iv. 22). Such
instances are numerous. In accordance with this metaphor, true
principles are called life, and corrupt principles death. Thus the
Almighty says, “See, I have set before thee this day life and good
and death and evil” (Deut. xxx. 15), showing that “life” and “good,”
“death” and “evil,” are identical, and then He explains these terms.
In the same way I understand His words, “That ye may live” (ib. v.
33), in accordance with the traditional interpretation of “That it may
be well with thee” [scil. in the life to come] (ib. xxii. 7). In
consequence of the frequent use of this figure in our language our
Sages said, “The righteous even in death are called living, while the
wicked even in life are called dead.” (Talm. B. Berakhoth, p. 78).
Note this well.
[Contents]
CHAPTER XLIII
The Hebrew kanaf is a homonym; most of its meanings are
metaphorical. Its primary signification is “wing of a flying creature,”
e.g., “Any winged (kanaf) fowl that flieth in the air” (Deut. iv. 17).
It was also used to denote the ends of the inhabited part of the
earth, and the corners that are most distant from our habitation.
Comp. “That it might take hold of the ends (kanfoth) of the earth”
(Job xxxviii. 13); “From the uttermost part (kenaf) of the earth have
we heard songs” (Isa. xxiv. 16).
Ibn Ganaḥ (in his Book of Hebrew Roots) says that kenaf is used in
the sense of “concealing,” in analogy with the Arabic kanaftu
alshaian, “I have hidden something,” and accordingly explains,
Isaiah xxx. 20, “And thy teacher will no longer be hidden or
concealed.” It is a good explanation, and I think that kenaf has the
same meaning in Deuteronomy xxiii. 1, “He shall not take away the
cover (kenaf) of his father”; also in, “Spread, therefore, thy cover
(kenafeka) over thine handmaid” (Ruth iii. 9). In this sense, I think,
the word is figuratively applied to God and to angels (for angels are
not corporeal, according to my opinion, as I shall explain). Ruth ii.
12 must therefore be translated “Under whose protection (kenafav)
thou art come to trust”; and wherever the word occurs in reference
to angels, it means concealment. You have surely noticed the words
of Isaiah (Isa. vi. 2), “With twain he covered his face, and with twain
he covered his feet.” Their meaning is this: The cause of his (the
angel’s) existence is hidden and concealed; this is meant by the
[58]covering of the face. The things of which he (the angel) is the
cause, and which are called “his feet” (as I stated in speaking of the
homonym regel), are likewise concealed; for the actions of the
intelligences are not seen, and their ways are, except after long
study, not understood, on account of two reasons—the one of which
is contained in their own properties, the other in ourselves; that is to
say, because our perception is imperfect and the ideals are difficult
to be fully comprehended. As regards the phrase “and with twain he
flieth,” I shall explain in a special chapter (xlix.) why flight has been
attributed to angels.
[Contents]
CHAPTER XLIV
The Hebrew ʻayin is a homonym, signifying “fountain”; e.g., “By a
fountain (ʻen) of water” (Gen. xvi. 7). It next denotes “eye”; comp.
(ʻayin) “Eye for eye” (Exod. xxi. 24). Another meaning of the word is
“providence,” as it is said concerning Jeremiah, “Take him and direct
thine attention (eneka) to him” (Jer. xxxix. 12). In this figurative
sense it is to be understood when used in reference to God; e.g.,
“And my providence and my pleasure shall be there perpetually” (1
Kings ix. 3), as we have already explained (page 140); “The eyes
(ʻene), i.e., the Providence of the Lord thy God, are always upon it”
(Deut. xi. 12); “They are the eyes (ʻene) of the Lord, which run to
and fro through the whole earth” (Zech. iv. 10), i.e., His providence
is extended over everything that is on earth, as will be explained in
the chapters in which we shall treat of Providence. When, however,
the word “eye” is connected with the verb “to see,” (raah or ḥazah)
as in “Open thine eyes, and see” (1 Kings xix. 16); “His eyes behold”
(Ps. xi. 4), the phrase denotes perception of the mind, not that of
the senses; for every sensation is a passive state, as is well known
to you, and God is active, never passive, as will be explained by me.
[Contents]
CHAPTER XLV
Shamaʻ is used homonymously. It signifies “to hear,” and also “to
obey.” As regards the first signification, comp. “Neither let it be
heard out of thy mouth” (Exod. xxiii. 13); “And the fame thereof was
heard in Pharaoh’s house” (Gen. xlv. 16). Instances of this kind are
numerous.
Equally frequent are the instances of this verb being used in the
sense of “to obey”: “And they hearkened (shameʻü) not unto Moses”
(Exod. vi. 9). “If they obey (yishmeʻü) and serve him” (Job xxxvi.
11); “Shall we then hearken (nishmaʻ) unto you” (Neh. xiii. 27);
“Whosoever will not hearken (yishmaʻ) unto thy words” (Josh. i. 18).
The verb also signifies “to know” (“to understand”), comp. “A nation
whose tongue, i.e., its language, thou shalt not understand”
(tishmaʻ) (Deut. xxviii. 49). The verb shamaʻ, used in reference to
God, must be taken in the sense of perceiving, which is part of the
third signification, whenever, according to the literal interpretation of
the passage, it appears to have the first meaning: comp. “And the
Lord heard it” (Num. xi. 1); “For that He heareth your murmurings”
(Exod. xvi. 7). In all such passages mental perception is meant.
When, however, according to the literal interpretation [59]the verb
appears to have the second signification, it implies that God
responded to the prayer of man and fulfilled his wish, or did not
respond and did not fulfil his wish: “I will surely hear his cry” (Exod.
xxii. 23); “I will hear, for I am gracious” (ib. 27); “Bow down thine
ear, and hear” (2 Kings xix. 16); “But the Lord would not hearken to
your voice, nor give ear unto you” (Deut. i. 45); “Yea, when ye make
many prayers, I will not hear” (Isa. i. 15); “For I will not hear thee”
(Jer. vii. 16). There are many instances in which shamaʻ has this
sense.
[Contents]
CHAPTER XLVI
We have already stated, in one of the chapters of this treatise, that
there is a great difference between bringing to view the existence of
a thing and demonstrating its true essence. We can lead others to
notice the existence of an object by pointing to its accidents,
actions, or even most remote relations to other objects: e.g., if you
wish to describe the king of a country to one of his subjects who
does not know him, you can give a description and an account of his
existence in many ways. You will either say to him, the tall man with
a fair complexion and grey hair is the king, thus describing him by
his accidents; or you will say, the king is the person round whom are
seen a great multitude of men on horse and on foot, and soldiers
with drawn swords, over whose head banners are waving, and
before whom trumpets are sounded; or it is the person living in the
palace in a particular region of a certain country; or it is the person
who ordered the building of that wall, or the construction of that
bridge; or by some other similar acts and things relating to him. His
existence can be demonstrated in a still more indirect way, e.g., if
you are asked whether this land has a king, you will undoubtedly
answer in the affirmative. “What proof have you?” “The fact that this
banker here, a weak and little person, stands before this large mass
of gold pieces, and that poor man, tall and strong, who stands
before him asking in vain for alms of the weight of a carob-grain, is
rebuked and is compelled to go away by the mere force of words;
for had he not feared the king, he would, without hesitation, have
killed the banker, or pushed him away and taken as much of the
money as he could.” Consequently, this is a proof that this country
has a ruler and his existence is proved by the well-regulated affairs
of the country, on account of which the king is respected and the
punishments decreed by him are feared. In this whole example
nothing is mentioned that indicated his characteristics, and his
essential properties, by virtue of which he is king. The same is the
case with the information concerning the Creator given to the
ordinary classes of men in all prophetical books and in the Law. For
it was found necessary to teach all of them that God exists, and that
He is in every respect the most perfect Being, that is to say, He
exists not only in the sense in which the earth and the heavens
exist, but He exists and possesses life, wisdom, power, activity, and
all other properties which our belief in His existence must include, as
will be shown below. That God exists was therefore shown to
ordinary [60]men by means of similes taken from physical bodies;
that He is living, by a simile taken from motion, because ordinary
men consider only the body as fully, truly, and undoubtedly existing;
that which is connected with a body but is itself not a body, although
believed to exist, has a lower degree of existence on account of its
dependence on the body for existence. That, however, which is
neither itself a body, nor a force within a body, is not existent
according to man’s first notions, and is above all excluded from the
range of imagination. In the same manner motion is considered by
the ordinary man as identical with life; what cannot move voluntarily
from place to place has no life, although motion is not part of the
definition of life, but an accident connected with it. The perception
by the senses, especially by hearing and seeing, is best known to us;
we have no idea or notion of any other mode of communication
between the soul of one person and that of another than by means
of speaking, i.e., by the sound produced by lips, tongue, and the
other organs of speech. When, therefore, we are to be informed that
God has a knowledge of things, and that communication is made by
Him to the Prophets who convey it to us, they represent Him to us
as seeing and hearing, i.e., as perceiving and knowing those things
which can be seen and heard. They represent Him to us as
speaking, i.e., that communications from Him reach the Prophets;
that is to be understood by the term “prophecy,” as will be fully
explained. God is described as working, because we do not know
any other mode of producing a thing except by direct touch. He is
said to have a soul in the sense that He is living, because all living
beings are generally supposed to have a soul; although the term
soul is, as has been shown, a homonym.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookgate.com