Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
92
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

Introduction

C C C

 

CCC

 

C

C

C

Acknowledgments

I would like to offer my thanks to the following organizations and people for their support, help, guidance, and enthusiasm.

The Sams editorial and production staff, especially Gregory Croy, Stacy Hiquet, Susan Pink, Mary Corder, and Rebecca Whitney, all who put enormous effort into making this a good book. I would also like to thank Timothy C. Moore, who did the technical editing.

Borland International Inc., Microsoft Corporation, and Watcom Products, Inc., have provided valuable support and assistance.

Thanks to William Colley, III, and the C User’s Group, for the Highly Portable Utilities (CUG-236) files that are included on the sample source diskette.

Eric Jackson (“Eric in the Evening”) and public radio station WGBH for providing all the jazz.

Thank you all.

xxi

Advanced C

xxii

Introduction

C C C

 

CCC

 

C

C

C

Introduction

C has become one of the most frequently used computer languages. The first C language was developed by Dennis Ritchie at Bell Laboratories in 1972 and ran on a DEC PDP-11. The ANSI standard for C, which replaced the standard written by Kernighan and Ritchie in 1978, is only a few years old.

C’s structure is similar to PL/I (a popular language used on IBM’s mainframe computers), FORTRAN, Pascal, and BASIC. C is a simple language. It has only a small group of keywords and no support for I/O or advanced math. The power of C comes from its simplicity and its use of a standard library of functions.

Who Should Read This Book?

Advanced C is for the programmer who has some experience writing applications in C or a similar language, such as PL/I or Pascal. Regardless of whether you are an intermediate or experienced programmer, this book is intended to improve your skills as easily as possible.

What Is in This Book?

This book has several purposes. First, it introduces advanced parts of the C language. It also describes changes in the ANSI standard, which is the only true definition of the C language. In addition, the book contains much of what I have learned (often the hard way) about C programming.

Advanced C is divided into five parts, and each part can be used by itself. Part I gets you started and lays the groundwork for the rest of the book. In Part II, you learn how to manage data and files when programming in C. Part III introduces integrating C with other languages and interfacing with other environments such as database programs. Part IV is a reference section that covers the header files, the intrinsic functions, the preprocessor, and some performance and debugging techniques. Part V

xxiii

Advanced C

Introduction

 

 

(the appendixes) contains an ASCII table, information about different compilers, an introduction to C++, and a cross-reference of functions and their header files.

Many chapters contain example programs. In some chapters, a single example program is used to demonstrate several topics in the chapter.

For a platform to develop C software, I recommend at least a 386/25, and preferably a 386/33 or 486. A 286 will do, but most linkers and some compilers are noticeably slower when you do not have a fast CPU. I suggest that you have at least a 100M hard disk. The compiler I use most frequently is QuickC for Windows. It is powerful and easy to use (because it has an integrated debugging environment), and supports both ANSI C and Microsoft’s extensions.

Conventions Used in This Book

I used the following conventions in the book:

All program listings and code fragments are in monospace.

All function names are in monospace.

ANSI C keywords are in monospace.

All function names appearing in text (not in the code) are followed by an empty set of parentheses, for example, sprintf().

Something that must be substituted (such as a filename or a value) is in

monospace italic.

When a listing title shows a filename in uppercase, that file is usually found on the sample diskette. If a filename is not given or it is in lowercase, then it is not a separate source file on the diskette, but probably part of another file on the sample diskette. The text usually indicates which file the code fragment is from.

A Note on Practicing C

You can read, attend lectures, or discuss a subject, but as the saying goes, “practice makes perfect.”

xxiv

Introduction

C C C

 

CCC

 

C

C

C

Do not be afraid to practice with the programs in this book. But practice does not mean copying a program from the diskette, compiling it, and running it. Change the example programs. Make them do things they weren’t intended to do and learn from your mistakes. Make backups often and program away. Because C is a powerful language and many of us are programming on PCs using DOS (which has very poor memory protection), be careful; it is easy to trash the disk.

Good luck improving your C programming skills, have fun writing your software, and remember Peter’s rule: Back up your disk frequently!

xxv

Advanced C

xxvi

Part I

Honing

Your C Skills

1

Advanced C

2

C TheCPhilosophyCC C C

C1C C

1 C C C

C C C

C C C

The C Philosophy

C probably wasn’t your first computer language. Mine was FORTRAN, and many other people began their study of computer language with either BASIC or PASCAL. No matter which language was your first, you probably will spend much time programming in C from now on. This chapter covers a number of introductory topics.

A Brief History of C and the Standard

Until the past few years, no absolute standard for the C language existed. The C Programming Language, by Kernighan and Ritchie, served as a standard, but most compiler manufacturers added extensions and did not follow all the specifications presented by Kernighan and Ritchie. As C became one of the most popular computer languages for programming small computers, the need for a true standard became apparent.

3

Part I • Honing Your C Skills

The American National Standards Institute (ANSI) produced standards that help keep each of the compilers working in the same manner. These standards, which are very exacting, spell out exactly what the language should do and what should not happen. Specified limits and definitions exist also.

C is an interesting language. Because its syntax is simple, it’s not the most powerful language, and it has only a few operations. Most of C’s power comes from these attributes:

C can address and manipulate memory by direct address. A program can obtain the memory address of any object (both data objects and functions) and manipulate without restriction the contents of the memory specified by the address. This capability is good to have because it allows flexibility. However, you have no protection from the program overwriting critical parts of the operating system when you are programming a PC using DOS.

C has a powerful library of functions. This library of functions enables programmers to perform I/O, work with strings (which are arrays of characters), and perform many other tasks.

There is a lot of talk (much I consider to be blown out of proportion) about portability. Generally, for each program, you should consider whether it is likely to be needed on a different system, and how much effort must be dedicated to planning the move to a future system. Some C programming is never portable. Programs written for Microsoft Windows, for example, don’t move well to the Apple Macintosh or IBM’s OS/2 Presentation Manager (a system much like Windows). The decision to maintain portability is one that you must make—sometimes the effort to maintain portability far exceeds what is required if later parts of the program must be rewritten.

The ANSI standard specified a number of language limits (see Table 1.1). Many of these limits are really compiler limits; however, because they affect the language, you sometimes must take them into consideration. These limits are not usually a problem; in the ten years that I’ve been writing C programs, I’ve run into problems with these limits only once or twice.

4