Cover image for Practical C Programming, 3rd Edition

Book description

There are lots of introductory C books, but this is the first one that has the no-nonsense, practical approach that has made Nutshell Handbooks® famous. C programming is more than just getting the syntax right. Style and debugging also play a tremendous part in creating programs that run well and are easy to maintain. This book teaches you not only the mechanics of programming, but also describes how to create programs that are easy to read, debug, and update. Practical rules are stressed. For example, there are fifteen precedence rules in C (&& comes before || comes before ?:). The practical programmer reduces these to two:

  • Multiplication and division come before addition and subtraction.

Contrary to popular belief, most programmers do not spend most of their time creating code. Most of their time is spent modifying someone else's code. This books shows you how to avoid the all-too-common obfuscated uses of C (and also to recognize these uses when you encounter them in existing programs) and thereby to leave code that the programmer responsible for maintenance does not have to struggle with. Electronic Archaeology, the art of going through someone else's code, is described. This third edition introduces popular Integrated Development Environments on Windows systems, as well as UNIX programming utilities, and features a large statistics-generating program to pull together the concepts and features in the language.

Table of Contents

  1. Practical C Programming, 3rd Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. How This Book is Organized
    2. Chapter by Chapter
    3. Notes on the Third Edition
    4. Font Conventions
    5. Obtaining Source Code
      1. FTP
      2. FTPMAIL
    6. Comments and Questions
    7. Acknowledgments
    8. Acknowledgments to the Third Edition
  4. I. Basics
    1. 1. What Is C?
      1. How Programming Works
      2. Brief History of C
      3. How C Works
      4. How to Learn C
    2. 2. Basics of Program Writing
      1. Programs from Conception to Execution
        1. Wrappers
      2. Creating a Real Program
      3. Creating a Program Using a Command-Line Compiler
        1. Step 1. Create a Place for Your Program
        2. Step 2. Create the Program
        3. Step 3. Run the Compiler
          1. UNIX cc compiler (generic UNIX)
          2. Free Software Foundation’s gcc compiler
          3. Borland’s Turbo C++ under MS-DOS
          4. Borland C++ under MS-DOS and Windows
          5. Microsoft Visual C++
        4. Step 4. Execute the Program
      4. Creating a Program Using an Integrated Development Environment
        1. Step 1. Create a Place for Your Program
        2. Step 2. Enter, Compile, and Run Your Program Using the IDE
          1. Turbo C++
          2. Borland C++
          3. Microsoft Visual C++
      5. Getting Help on UNIX
      6. Getting Help in an Integrated Development Environment
      7. IDE Cookbooks
        1. Turbo C++
        2. Borland C++
        3. Microsoft Visual C++
      8. Programming Exercises
    3. 3. Style
      1. Common Coding Practices
      2. Coding Religion
      3. Indentation and Code Format
      4. Clarity
      5. Simplicity
      6. Summary
    4. 4. Basic Declarations and Expressions
      1. Elements of a Program
      2. Basic Program Structure
      3. Simple Expressions
      4. Variables and Storage
      5. Variable Declarations
      6. Integers
      7. Assignment Statements
      8. printf Function
      9. Floating Point
      10. Floating Point Versus Integer Divide
      11. Characters
      12. Answers
      13. Programming Exercises
    5. 5. Arrays, Qualifiers, and Reading Numbers
      1. Arrays
      2. Strings
      3. Reading Strings
      4. Multidimensional Arrays
      5. Reading Numbers
      6. Initializing Variables
      7. Types of Integers
      8. Types of Floats
      9. Constant Declarations
      10. Hexadecimal and Octal Constants
      11. Operators for Performing Shortcuts
      12. Side Effects
      13. ++x or x++
      14. More Side-Effect Problems
      15. Answers
      16. Programming Exercises
    6. 6. Decision and Control Statements
      1. if Statement
      2. else Statement
      3. How Not to Use strcmp
      4. Looping Statements
      5. while Statement
      6. break Statement
      7. continue Statement
      8. Assignment Anywhere Side Effect
      9. Answer
      10. Programming Exercises
    7. 7. Programming Process
      1. Setting Up
      2. Specification
      3. Code Design
      4. Prototype
      5. Makefile
        1. Generic UNIX
        2. UNIX with the Free Software Foundation’s gcc Compiler
        3. Borland C++
        4. Turbo C++
        5. Visual C++
      6. Testing
      7. Debugging
      8. Maintenance
      9. Revisions
      10. Electronic Archaeology
      11. Marking Up the Program
      12. Using the Debugger
      13. Text Editor as a Browser
      14. Add Comments
      15. Programming Exercises
  5. II. Simple Programming
    1. 8. More Control Statements
      1. for Statement
      2. switch Statement
      3. switch, break, and continue
      4. Answers
      5. Programming Exercises
    2. 9. Variable Scope and Functions
      1. Scope and Class
      2. Functions
      3. Functions with No Parameters
      4. Structured Programming
      5. Recursion
      6. Answers
      7. Programming Exercises
    3. 10. C Preprocessor
      1. #define Statement
        1. #define vs. const
      2. Conditional Compilation
      3. include Files
      4. Parameterized Macros
      5. Advanced Features
      6. Summary
      7. Answers
      8. Programming Exercises
    4. 11. Bit Operations
      1. Bit Operators
      2. The and Operator (&)
      3. Bitwise or (|)
      4. The Bitwise Exclusive or (^)
      5. The Ones Complement Operator (Not) (~)
      6. The Left- and Right-Shift Operators (<<, >>)
        1. Right-Shift Details
      7. Setting, Clearing, and Testing Bits
      8. Bitmapped Graphics
      9. Answers
      10. Programming Exercises
    5. 12. Advanced Types
      1. Structures
      2. Unions
      3. typedef
      4. enum Type
      5. Casting
      6. Bit Fields or Packed Structures
      7. Arrays of Structures
      8. Summary
      9. Programming Exercises
    6. 13. Simple Pointers
      1. Pointers as Function Arguments
      2. const Pointers
      3. Pointers and Arrays
      4. How Not to Use Pointers
      5. Using Pointers to Split a String
      6. Pointers and Structures
      7. Command-Line Arguments
      8. Programming Exercises
      9. Answers
    7. 14. File Input/Output
      1. Conversion Routines
      2. Binary and ASCII Files
      3. The End-of-Line Puzzle
      4. Binary I/O
      5. Buffering Problems
      6. Unbuffered I/O
      7. Designing File Formats
      8. Answers
      9. Programming Exercises
    8. 15. Debugging and Optimization
      1. Debugging
        1. Divide and Conquer
        2. Debug-Only Code
        3. Debug Command-Line Switch
        4. Going Through the Output
      2. Interactive Debuggers
      3. Debugging a Binary Search
      4. Runtime Errors
      5. The Confessional Method of Debugging
      6. Optimization
        1. The Power of Powers of 2
        2. How to Optimize
      7. Answers
      8. Programming Exercises
    9. 16. Floating Point
      1. Floating-Point Format
      2. Floating Addition/Subtraction
      3. Multiplication
      4. Division
      5. Overflow and Underflow
      6. Roundoff Error
      7. Accuracy
      8. Minimizing Roundoff Error
      9. Determining Accuracy
      10. Precision and Speed
      11. Power Series
      12. Programming Exercises
  6. III. Advanced Programming Concepts
    1. 17. Advanced Pointers
      1. Pointers and Structures
      2. free Function
      3. Linked List
      4. Structure Pointer Operator
      5. Ordered Linked Lists
      6. Double-Linked Lists
      7. Trees
      8. Printing a Tree
      9. Rest of Program
      10. Data Structures for a Chess Program
      11. Answers
      12. Programming Exercises
    2. 18. Modular Programming
      1. Modules
      2. Public and Private
      3. The extern Modifier
      4. Headers
      5. The Body of the Module
      6. A Program to Use Infinite Arrays
      7. The Makefile for Multiple Files
      8. Using the Infinite Array
        1. Makefile for UNIX Generic C
        2. Makefile for Free Software Foundation’s gcc
        3. Makefile for Turbo C++
        4. Makefile for Borland C++
        5. Makefile for Microsoft Visual C++
      9. Dividing a Task into Modules
      10. Module Division Example: Text Editor
      11. Compiler
      12. Spreadsheet
      13. Module Design Guidelines
      14. Programming Exercises
    3. 19. Ancient Compilers
      1. K&R-Style Functions
        1. Function Prototypes
      2. Library Changes
      3. Missing Features
      4. Free/Malloc Changes
      5. lint
      6. Answers
    4. 20. Portability Problems
      1. Modularity
      2. Word Size
      3. Byte Order Problem
      4. Alignment Problem
      5. NULL Pointer Problem
      6. Filename Problems
      7. File Types
      8. Summary
      9. Answers
    5. 21. C’s Dustier Corners
      1. do/while
      2. goto
      3. The ?: Construct
      4. The , Operator
      5. volatile Qualifier
      6. Answer
    6. 22. Putting It All Together
      1. Requirements
      2. Specification
      3. Code Design
        1. Token Module
        2. Input Module
          1. How not to design an input module
          2. A better input module
        3. Character Type Module
        4. Statistics Submodules
      4. Coding
      5. Functional Description
        1. ch_type Module
        2. in_file Module
        3. token Module
        4. Line Counter Submodule (lc)
        5. Brace Counter Submodule (bc)
        6. Parentheses Counter Submodule (pc)
        7. Comment Counter Submodule (cc)
        8. do_file Procedure
      6. Expandability
      7. Testing
      8. Revisions
      9. A Final Warning
      10. Program Files
        1. The in_file.h File
        2. The in_file.c File
        3. The ch_type.h File
        4. The ch_type.c File
        5. The token.h File
        6. The token.c File
        7. The stat.c File
        8. UNIX Makefile for CC (Generic Unix)
        9. UNIX Makefile for gcc
        10. Turbo C++ Makefile
        11. Borland C++ Makefile
        12. Microsoft Visual C++ Makefile
      11. Programming Exercises
    7. 23. Programming Adages
      1. General
      2. Design
      3. Declarations
      4. switch Statement
      5. Preprocessor
      6. Style
      7. Compiling
      8. Final Note
      9. Answer
  7. IV. Other Language Features
    1. A. ASCII Table
    2. B. Ranges and Parameter Passing Conversions
      1. Ranges
      2. Automatic Type Conversions to Use When Passing Parameters
    3. C. Operator Precedence Rules
      1. Standard Rules
      2. Practical Subset
    4. D. A Program to Compute a Sine Using a Power Series
      1. The sine.c Program
  8. Glossary
  9. Index
  10. About the Author
  11. Colophon
  12. Copyright