C for Scientists and Engineers Richard Johnsonbaugh Martin Kalin DePaul University Table of Contents 1 Computer Systems and Program Development 1.1 Algorithms 1.2 Computer Systems 1.3 Internal Representations 1.4 Programming Languages 1.5 Program Development 1.6 Why C? 2 Introduction to C 2.1 A First C Program 2.2 Real World Application: Computing Distances 2.3 Identifiers 2.4 The while Statement 2.5 The do while Statement 2.6 Real World Application: Classifying Solutions as Acidic or Nonacidic 2.7 The if Statement 2.8 More on the if Statement 2.9 Redirecting Input and Output 2.10 Files Common Programming Errors Programming Exercises 3 Variables, Operators, and Control Flow 3.1 Characters and Integers 3.2 Floating-Point Variables 3.3 Arithmetic Operations 3.4 Relational and Logical Operators and the Assignment Operator 3.5 Real World Application: Statistical Measures 3.6 The for Statement and the Comma Operator 3.7 The Operators ++ and -- 3.8 Real World Application: Printing a Bar Graph Common Programming Errors Programming Exercises 4 More Operators and Control Flow 4.1 The break and continue Statements 4.2 Real World Application: Generating Prime Numbers 4.3 The switch Statement 4.4 The goto Statement and Labels 4.5 Conditional Expressions 4.6 Real World Application: Printing a Calendar 4.7 The Cast Operator 4.8 Real World Application: Summing a Series 4.9 The sizeof Operator 4.10 getchar and putchar 4.11 Bitwise Operators Common Programming Errors Programming Exercises 5 Functions and Program Structure 5.1 Introduction 5.2 Arguments and Parameters 5.3 Call by Value 5.4 Real World Application: Computing Resistance 5.5 The Scope of Variables 5.6 The Preprocessor 5.7 Real World Application: Monte Carlo Integration 5.8 Recursion 5.9 Real World Application: Recursive Tiling 5.10 Functions with an Arbitrary Number of Arguments Common Programming Errors Programming Exercises 6 Arrays 6.1 Why Arrays? 6.2 Array Indexes and Cell Offsets 6.3 Real World Application: The Fourier Transform 6.4 Character Strings as Arrays of Characters 6.5 Arrays as Function Arguments 6.6 String-Handling Functions 6.7 Real World Application: Computing a String's Length 6.8 Multidimensional Arrays 6.9 Real World Application: Matrix Multiplication 6.10 Real World Application: Solving a Linear System of Equations 6.11 Real World Application: Sorting and Searching 6.12 Real World Application: Forest Fire Percolation Common Programming Errors Programming Exercises 7 Pointers 7.1 Pointer Variables 7.2 Levels of Indirection 7.3 Pointers and Arrays 7.4 Pointers as Arguments to Functions 7.5 Real World Application: Reversing a String in Place 7.6 Pointers and Multidimensional Arrays 7.7 Command Line Arguments 7.8 Pointers to Functions 7.9 Real World Application: Comparing Sorting Algorithms 7.10 Real World Application: Sorting and Searching Revisited Common Programming Errors Programming Exercises 8 Storage Classes and Type Qualifiers 8.1 Storage Classes in a Single-Source File: auto, extern, static 8.2 The Storage Class register 8.3 Storage Classes in Multiple-Source Files 8.4 Real World Application: An Interactive Calculator 8.5 Nested Blocks 8.6 Storage Classes for Functions 8.7 Type Qualifiers: const and volatile 8.8 Real World Application: A Scheduling Problem Common Programming Errors Programming Exercises 9 Input and Output 9.1 Opening and Closing Files 9.2 Character Input/Output 9.3 Real World Application: Determining a Source File's Size in Bytes 9.4 String Input/Output 9.5 Formatted Input/Output 9.6 Unformatted Input/Output 9.7 Moving Around in a File: fseek, ftell, rewind 9.8 Real World Application: A Random Access File Common Programming Errors Programming Exercises 10 Structures, Unions, and Enumerated Types 10.1 Introduction to Structures 10.2 Real World Application: Complex Numbers 10.3 The typedef Construct 10.4 Operations on Structures 10.5 Pointers to Structures, Nested Structures, and Self- Referential Structures 10.6 Structures and Functions 10.7 Real World Application: Chemical Synthesis 10.8 Unions and Bit Fields 10.9 Enumerated Types Common Programming Errors Programming Exercises 11 Advanced Topics 11.1 Compile-Time and Run-Time Storage Allocation 11.2 Real World Application: Matrix Multiplication Revisited 11.3 Linked Lists 11.4 Stacks and Queues 11.5 Assertions 11.6 Exception-Handling and Jumps 11.7 Graphics Support for C 11.8 Real World Application: Fractals 11.9 Looking Toward C++ Common Programming Errors Programming Exercises Appendix A. ASCII and EBCDIC Tables B. Summary of the C Language C. Syntax Diagrams of C D. Some C Functions E. C and UNIX F. VAX-11 C, Borland C++, and Microsoft C++ Hints and Solutions to Odd-Numbered Exercises Index