free-e-book-computer-science-advanced-c
Free download
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
contents
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
Advanced C
viii
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
Introduction
ix
C C
C 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
Advanced C
x
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
Introduction
xi
C C
C C
C
C
C
C C C
Part II: Managing Data in C 189
7 C Structures ………………………………………………………………… 191
Using the struct Keyword ……………………………………………………191
Arrays of Structures ……………………………………………………………195
Structures of Arrays ……………………………………………………………200
Structures of Structures ………………………………………………………203
Bit Fields in Structures ……………………………………………………….206
Using the typedef Keyword …………………………………………………208
Using the offsetof() Macro ………………………………………………….213
Pointers to Structures …………………………………………………………216
Understanding unions ………………………………………………………..219
Summary………………………………………………………………………….226
8 Dynamic Memory Allocation…………………………………………. 227
Using the malloc( ) Function……………………………………………….228
Using the calloc( ) Function ………………………………………………..232
Using the free( ) Function …………………………………………………..235
Using the realloc( ) Function ……………………………………………….237
Allocating Arrays ……………………………………………………………….244
Global Memory versus Local Memory …………………………………..247
Summary………………………………………………………………………….248
9 Disk Files and Other I/O………………………………………………. 249
File I/O Basics …………………………………………………………………..250
Text Files and Binary Files ………………………………………………….251
Creating and Using Temporary Work Files ……………………………256
Stream Files and Default File Handles …………………………………..268
The stdin File ………………………………………………………………..271
The stdout File ……………………………………………………………..272
The stderr File ………………………………………………………………272
The stdaux File ……………………………………………………………..273
The stdprn File ……………………………………………………………..274
Low-Level I/O and File Handles ………………………………………….278
Standard Low-Level File Handles …………………………………………280
Console and Port I/O…………………………………………………………280
Direct Port I/O …………………………………………………………………288
Table of Contents
Advanced C
xii
The PC Printer Ports………………………………………………………….289
The PC Communications Ports …………………………………………..296
Summary………………………………………………………………………….318
10 Data Management: Sorts, Lists, and Indexes ……………………. 321
Sorting …………………………………………………………………………….322
Merging …………………………………………………………………………..329
Purging ……………………………………………………………………………336
Sorting, Merging, and Purging All in One ……………………………..343
Linked Lists ………………………………………………………………………344
Using Dynamic Memory ………………………………………………..345
Disk-Based Lists …………………………………………………………….346
Double Linked Lists ……………………………………………………….346
Indexing …………………………………………………………………………..367
Fixed-field Disk Files ………………………………………………………….392
B-trees ……………………………………………………………………………..392
Summary………………………………………………………………………….430
Part III: Working with Others …………………………………………………………………. 433
11 C and Other Languages ………………………………………………… 435
Other Languages ……………………………………………………………….436
Assembly ………………………………………………………………………438
FORTRAN…………………………………………………………………..441
Pascal …………………………………………………………………………..442
BASIC …………………………………………………………………………443
Calling Other Languages from C………………………………………….443
Calling Assembly from C ………………………………………………..447
Calling FORTRAN and Pascal from C ……………………………..449
Calling C Functions from Other Languages …………………………..450
Calling C from Assembly ………………………………………………..451
Calling C from FORTRAN and Pascal ……………………………..462
All the Things that Can Go Wrong ………………………………………462
Looking at Data …………………………………………………………….463
Names and Limits ………………………………………………………….465
Summary………………………………………………………………………….465
Introduction
xiii
C C
C C
C
C
C
C C C
12 C and Databases ………………………………………………………….. 467
Interfacing with dBASE-Compatible Programs ………………………468
Using dBASE Files Directly…………………………………………………468
Reading dBASE and dBASE-Compatible Files ……………………474
Creating dBASE and dBASE-Compatible Files …………………..484
Updating dBASE and dBASE-Compatible Files ………………….494
Summary………………………………………………………………………….494
NORDIC SEMICONDUCTOR -on-good-rfic-product-company free-e-book-computer-science-c-compiler-reference
