C: The Complete Reference by Herbert Schildt, Paperback | Barnes & Noble
C: The Complete Reference

C: The Complete Reference

by Herbert Schildt
     
 
Whether you are a beginning C programmer or a seasoned pro, the answers to all your C questions can be found in this one-stop resource.

To help you with quick fact-finding, C: The Complete Reference, Third Edition is divided into five parts, each covering an important aspect of C.

Overview

Whether you are a beginning C programmer or a seasoned pro, the answers to all your C questions can be found in this one-stop resource.

To help you with quick fact-finding, C: The Complete Reference, Third Edition is divided into five parts, each covering an important aspect of C.

Editorial Reviews

Booknews
A guide to C++ by one of the standard authors. Covers features common to C & C++ features, specific C++ features, and software development (with concrete examples). Annotation c. Book News, Inc., Portland, OR (booknews.com)

Product Details

ISBN-13:
9780078821011
Publisher:
McGraw-Hill Osborne
Publication date:
09/28/2003
Edition description:
Older Edition
Pages:
821
Product dimensions:
7.41(w) x 9.07(h) x 1.74(d)

Read an Excerpt


Chapter 2: EXPRESSIONS

This chapter examines the most fundamental element of the C (as well as the C++) language: the expression. As you will see, expressions in C/C++ are substantially more general and more powerful than in most other computer languages. Expressions are formed from these atomic elements: data and operators. Data may be represented either by variables or by constants. Like most other computer languages, C/C++ supports a number of different types of data. It also provides a wide variety of operators.

The Five Basic Data Types

There are five atomic data types in C: character, integer, floating-point, and valueless (char, int, float, double, and void, respectively). As you will see, all other data types in C are based upon one of these types. The size and range of these data types may vary between processor types and compilers. However, in all cases a character is 1 byte. The size of an integer is usually the same as the word length of the execution environment of the program. For most 16-bit environments, such as DOS or Windows 3.1, an integer is 16 bits. For most 32-bit environments, such as Windows NT, an integer is 32 bits. However, you cannot make assumptions about the size of an integer if you want your programs to be portable to the widest range of environments. It is important to understand that both C and C++ only stipulate the minimal range of each data type, not its size in bytes.

NOTE: To the five basic data types defined by C, C++ adds two more. bool and wchar_t. These are discussed in Part Two.

implemented. Integers will generally correspond to the natural size of a word on the host computer. Values of type char are generally used to hold values defined by the ASCII character set. Values outside that range may be handled differently by different compilers.

the floating- point numbers. Whatever the method, the range is quite large. Standard C specifies that the minimum range for a floating-point value is 1E-37 to 1E+37. The minimum number of digits of precision for each floating-point type is shown in Table 2-1.

NOTE: Standard C++ does not specify a minimum size or range for the basic types. Instead, it simply states that they must meet certain requirements. For example, Standard C++ states that an int will "have the natural size suggested by the architecture of the execution environment." In all cases, this will meet or exceed the minimum ranges specified by Standard C. Each C++ compiler specifies the size and range of the basic types in the header <climits>. preceding them. situation more precisely. The list of modifiers is shown here:

base types. You can apply unsigned and signed to characters. You may also apply long to double. Table 2-1 shows all valid data type combinations, along with their minimal ranges and approximate bit widths. (These values also apply to a typical C++ implementation.) Remember, the table shows the minimum range that these types win have as specified by Standard C/C++, not their typical range. For example, on computers that use two's complement arithmetic (which is nearly all), an integer will have a range of at least 32,767 to -32,768.

integer declaration assumes a signed number. The most important use of signed is to modify char in implementations in which char is unsigned by default.

highorder bit of the integer is interpreted. If you specify a signed integer, the compiler generates code that assumes that the high-order bit of an integer is to be used as a sign flag. If the sign flag is 0, the number is positive; if it is 1, the number is negative.

approach, which reverses all bits in the number (except the sign flag), adds 1 to this number, and sets the sign flag to 1.

have half the absolute magnitude of their unsigned relatives. For example, here is 32,767:

If the high-order bit were set to 1, the number would be interpreted as However, if you declare this to be an unsigned int, the number becomes 65,535 when the high order bit is set to 1.

Identifier Names

In C/C++, the names of variables, functions, labels, and various other user-defined objects are called identifiers. These identifiers can vary from one to several characters. The first character must be a letter or an underscore, and subsequent characters must be either letters, digits, or underscores. Here are some correct and incorrect identifier names:

Correct Incorrect
Count 1 count
test23 hi! there

high_balance high balance

necessarily be significant. If the identifier will be involved in an external fink process, then at least the first six characters will be significant. These identifiers, called external names, include function names and global variables that are shared between files. If the identifier is not used in an external link process, then at least the first 31 characters will be significant. This type of identifier is called an internal name and includes the names of local variables, for example. In C++, there is no limit to the length of an identifier, and at least the first 1,024 characters are significant. This difference may be important if you are converting a program from C to C++.

count, Count, and COUNT are three separate identifiers.

the same name as functions that are in the C or C++ library.

Variables

As you probably know, a variable is a named location in memory that is used to hold a value that may be modified by the program. All variables must be declared before they can be used. The general form of a declaration is

type variable_list;

Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more identifier names separated by commas. Here are some declarations:

int i, j,1;

short int si;

unsigned int ui;

double balance, profit, loss;

Remember, in C/C++ the name of a variable has nothing to do with its type.

Where Variables Are Declared

Variables will be declared in three basic places: inside functions, in the definition of function parameters, and outside of all functions. These are local variables, formal parameters, and global variables.

Local Variables

some C/C++ book uses the more common term, local variable. Local variables may be referenced only by statements that are inside the block in which the variables are declared. In other words, local variables are not known outside their own code block. Remember, a block of code begins with an opening curly brace and terminates with a dosing curly brace.

declared is executing. That is, a local variable is created upon entry into its block and destroyed upon exit.

function. For example, consider, the following two functions:

void func1(void) {
int x;

}

void func2(void)
{

int x;

X = -199;

}

The integer variable x is declared twice, once in func1( ) and once in func2( ). The x in func1( ) has no bearing on or relationship to the x in func2( ). This is because each x is only known to the code within the same block as the variable declaration.

local variables. However, since all nonglobal variables are, by default, assumed to be auto, this keyword is virtually never used. Hence, the examples in this book will not use it. (It has been said that auto was included in C to provide for source-level compatibility with its predecessor B. Further, auto is supported in C++ to provide compatibility with C.)

variables used by a function immediately after the function's opening curly brace and before any other statements. However, you may declare local variables within any code block. The block defined by a function is simply a special case. For example,

void f (void)
{
int t;
scanf ( "%d%*c",&t);

if( t-=1) {

}

}

Here, the local variable s is created upon entry into the if code block and destroyed upon exit. Furthermore, s is known only within the if block and may not be referenced elsewhere even in other parts of the function that contains it.

that memory for the variable will only be allocated if needed. This is because local variables do not come into existence until the block in which they are declared is entered. You might need to worry about this when producing code for dedicated controllers (like a garage door opener that responds to a digital security code) in which RAM is in short supply, for example.

prevent unwanted side effects. Since the variable does not exist outside the block in which it is declared, it cannot be accidentally altered.

declare local variables. In C, you must declare all local variables at the start of the block in which they are defined, prior to any "action" statements. For example, the following function is in error if compiled by a C compiler.

/* This function is in error if compiled as a C program, but perfectly acceptable if compiled as a C++ program.
*/
void f(void)
{
j = 20;
}

However, in C++, this function is perfectly valid because you can define local variables at any point in your program. (The topic of C++ variable declaration is discussed in depth in Part Two.)

from the block in which they are declared, their content is lost once the block is left. This is especially important to remember when calling a function. When a function is called, its local variables are created, and upon its return they are destroyed. This means that local variables cannot retain their values between calls. (However, you can direct the compiler to retain their values by using the static modifier.)

fact that the stack is a dynamic and changing region of memory explains why local variables cannot, in general, hold their values between function calls. . . .

Meet the Author


Herbert Schildt is a leading authority on C and C++ and is a member of the ANSI/ISO C++ Standardization Committee. He has written numerous best-sellers, including C++ from the Ground Up, Expert C++, Teach Yourself C++, and many others.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >