Table Of ContentC C C
Introduction C
C C C
C C C
Advanced C
i
Advanced C
ii
C
C C C C C
Introduction C
C C C
C C C
C C C
C
C C
Advanced C
Peter D. Hipson
A Division of Prentice Hall Computer Publishing
201 W. 103rd St., Indianapolis, Indiana 46290 USA
iii
Advanced C
© 1992 by Sams Publishing
All rights reserved. Printed in the United States of America. No part of this book may be used or
reproduced in any form or by any means, or stored in a database or retrieval system, without prior
written permission of the publisher except in the case of brief quotations embodied in critical articles
and reviews. Making copies of any part of this book for any purpose other than your own personal
use is a violation of United States copyright laws. For information, address Sams Publishing, 201 W.
103rd St., Indianapolis, IN 46290
International Standard Book Number: 0-672-30168-7
Library of Congress Catalog Card Number: 92-061304
96 95 94 93 92 8 7 6 5 4 3
Interpretation of the printing code: the rightmost double-digit number is the year of the book’s
printing; the rightmost single-digit number, the number of the book’s printing. For example, a
printing code of 92-1 shows that the first printing of the book occurred in 1992.
Composed in AGaramond and MCPdigital by Prentice Hall Computer Publishing.
Screen reproductions in this book were created by means of the program Collage Plus,
from Inner Media, Inc., Hollis, NH.
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks
have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of
this information. Use of a term in this book should not be regarded as affecting the
validity of any trademark or service mark.
iv
C C C
Introduction C
C C C
C C C
Publisher Production Analyst
Richard K. Swadley Mary Beth Wakefield
Acquisitions Manager Book Design
Jordan Gold Michele Laseau
Managing Editor Cover Art
Neweleen A. Trebnik Tim Amrhein
Acquisitions Editor Graphic Images Specialist
Stacy Hiquet Dennis Sheehan
Production Editor Production
Mary Corder Katy Bodenmiller
Christine Cook
Technical Reviewer Lisa Daugherty
Timothy C. Moore Denny Hager
Carla Hall-Batton
Editorial Assistants John Kane
Roger Morgan
Rosemarie Graham
Juli Pavey
Lori Kelley
Angela Pozdol
Linda Quigley
Formatter
Michele Self
Pat Whitmer
Susan Shepard
Greg Simsic
Production Director
Alyssa Yesh
Jeff Valler
Index
Production Manager
Hilary Adams
Corinne Walls
Imprint Manager
Matthew Morrill
Proofreading/Indexing Coordinator
Joelynn Gifford
v
Advanced C
About the Author
Peter Hipson and his wife live and work in New Hampshire. He has worked with
computers since 1972, in hardware design and software development. He has
developed numerous software programs for both PCs and larger systems. He holds
patents in the field of CPU design and has been involved with microcomputers since
their inception. Peter is the developer of the Windows applications STARmanager and
STARmanager A/E.
You can contact Peter Hipson at P.O. Box 88, West Peterborough, NH, 03468.
Enclosing an SASE greatly enhances the likelihood of a reply.
To Bianca, who has shown me what great fun it is having a granddaughter.
vi
C C C
Introduction C
C C C
C C C
Overview
Introduction ..........................................................................xxiii
Part I Honing Your C Skills 1
1 The C Philosophy...................................................................... 3
2 Data Types, Constants, Variables, and Arrays ......................... 19
3 Pointers and Indirection.......................................................... 65
4 Special Pointers and Their Usage ............................................ 99
5 Decimal, Binary, Hex, and Octal........................................... 139
6 Separate Compilation and Linking........................................ 161
Part II Managing Data in C 189
7 C Structures........................................................................... 191
8 Dynamic Memory Allocation................................................. 227
9 Disk Files and Other I/O....................................................... 249
10 Data Management: Sorts, Lists, and Indexes ......................... 321
Part III Working with Others 433
11 C and Other Langauages........................................................ 435
12 C and Databases .................................................................... 467
13 All About Header Files........................................................... 497
vii
Advanced C
Part IV Documenting the Differences 519
14 ANSI C’s Library Functions................................................... 521
15 Preprocessor Directives.......................................................... 621
16 Debugging and Efficiency...................................................... 641
Part V Appendixes 677
A The ASCII Character Set ....................................................... 679
B Compiler Variations .............................................................. 681
C Introduction to C++ .............................................................. 695
D Function/Header File Cross Reference .................................. 723
Index...................................................................................... 741
viii
C C C
Introduction C
C C C
C C C
Contents
Introduction ..........................................................................xxiii
Part I: Honing Your C Skills................................................................................... 1
1 The C Philosophy...................................................................... 3
A Brief History of C and the Standard ............................................3
A Programming Style....................................................................11
Memory Models............................................................................17
Summary.......................................................................................18
2 Data Types, Constants, Variables, and Arrays ......................... 19
Data Types....................................................................................19
Constants......................................................................................25
Definitions versus Declarations .....................................................29
Declarations..............................................................................30
Definitions................................................................................33
Variables .......................................................................................35
Variable Types and Initializing Variables ..................................35
Scope (Or I Can See You).........................................................37
Life Span (Or How Long Is It Going To Be Here?)..................39
Type Casting ............................................................................41
Arrays............................................................................................46
Declaration of Arrays................................................................46
Definition of an Array...............................................................47
Array Indexing..........................................................................48
Using Array Names as Pointers.................................................55
Strings: Character Arrays...........................................................56
Using Arrays of Pointers ...........................................................58
Summary.......................................................................................62
ix
Advanced C
3 Pointers and Indirection.......................................................... 65
Pointers, Indirection, and Arrays...................................................65
Pointers.........................................................................................66
Indirection ....................................................................................69
An Example of Pointers, Indirection,
and Arrays...................................................................................69
Character Arrays and Strings.........................................................74
Indirection to Access Character Strings .........................................79
Protecting Strings in Memory .......................................................90
Ragged-Right String Arrays...........................................................92
Summary.......................................................................................98
4 Special Pointers and Their Use................................................ 99
Command Line Arguments...........................................................99
Function Pointers........................................................................114
Menus and Pointers.....................................................................120
State Machines............................................................................135
Summary.....................................................................................137
5 Decimal, Binary, Hex, and Octal........................................... 139
Decimal.......................................................................................139
Binary .........................................................................................141
Hex.............................................................................................142
Octal...........................................................................................144
Looking at a File .........................................................................146
Bit Operators ..............................................................................154
Bit Fields.....................................................................................155
Summary.....................................................................................158
6 Separate Compilation and Linking........................................ 161
Compiling and Linking Multiple Source Files.............................162
Compiling Multifile Programs ....................................................164
Linking Multifile Programs.........................................................164
Using #include............................................................................166
External Variables........................................................................171
Using an Object Library Manager...............................................181
Using MAKE Files......................................................................182
Summary.....................................................................................186
x