You are previewing C Primer Plus, Fourth Edition.
O'Reilly logo
C Primer Plus, Fourth Edition

Book Description

C Primer Plus is a conversational-style book aimed at those new to C programming. A friendly and easy-to-use self-study guide, this book will be appropriate for serious students of programming, as well as developers proficient in other languages with a desire to better understand the fundamentals of this core language.

As with all the editions, author Stephen Prata's aim has been to create an introduction to C that is instructive, clear, and helpful. Programming concepts are explained along with details of the C language; the book does not assume that you are a professional programmer. Many short, easily typed examples illustrate just one or two concepts at a time, since learning by doing is one of the most effective ways to master new information. Finally, review questions and programming exercises at the end of each chapter punctuate the most critical information and help readers digest the most difficult concepts. The text has been updated to accommodate the emergence of a new standard for C-C99.

Table of Contents

  1. Copyright
  2. PREFACE
  3. ABOUT THE AUTHOR
  4. ACKNOWLEDGMENTS
  5. TELL US WHAT YOU THINK!
  6. Getting Ready
    1. Whence C?
    2. Why C?
    3. Whither C?
    4. What Computers Do
    5. High-Level Computer Languages and Compilers
    6. Using C: Seven Steps
    7. Programming Mechanics
    8. Language Standards
    9. Book Organization
    10. Some Conventions
    11. Summary
    12. Review Questions
    13. Programming Exercise
  7. Introducing C
    1. A Simple Sample of C
    2. The Explanation
    3. The Structure of a Simple Program
    4. Tips on Making Your Programs Readable
    5. Taking Another Step
    6. While You're at It…Multiple Functions
    7. Debugging
    8. Keywords and Reserved Identifiers
    9. Key Concepts
    10. Summary
    11. Review Questions
    12. Programming Exercises
  8. Data and C
    1. A Sample Program
    2. Data Variables and Constants
    3. Data: Data-Type Keywords
    4. C Data Types
    5. Using Data Types
    6. Arguments and Pitfalls
    7. One More Example
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  9. Character Strings and Formatted Input/Output
    1. Introductory Program
    2. Character Strings: An Introduction
    3. Constants and the C Preprocessor
    4. Exploring and Exploiting printf() and scanf()
    5. Usage Tips
    6. Key Concepts
    7. Summary
    8. Review Questions
    9. Programming Exercises
  10. Operators, Expressions, and Statements
    1. Introducing Loops
    2. Fundamental Operators
    3. Some Additional Operators
    4. Expressions and Statements
    5. Type Conversions
    6. Function with Arguments
    7. A Sample Program
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  11. C Control Statements: Looping
    1. An Initial Example
    2. The while Statement
    3. Which Is Bigger: Using Relational Operators and Expressions
    4. Indefinite Loops and Counting Loops
    5. The for Loop
    6. More Assignment Operators: +=, -=, *=, /=, %=
    7. The Comma Operator
    8. An Exit-Condition Loop: do while
    9. Which Loop?
    10. Nested Loops
    11. Arrays
    12. A Loop Example Using a Function Return Value
    13. Key Concepts
    14. Summary
    15. Review Questions
    16. Programming Exercises
  12. C Control Statements: Branching and Jumps
    1. The if Statement
    2. Adding else to the if Statement
    3. Let's Get Logical
    4. A Word-Count Program
    5. The Conditional Operator: ?
    6. Loop Aids: continue and break
    7. Multiple Choice: switch and break
    8. The goto Statement
    9. Key Concepts
    10. Summary
    11. Review Questions
    12. Programming Exercises
  13. Character Input/Output and Input Validation
    1. Single-Character I/O: getchar() and putchar()
    2. Buffers
    3. Terminating Keyboard Input
    4. Redirection and Files
    5. Creating a Friendlier User Interface
    6. Input Validation
    7. Menu Browsing
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  14. Functions
    1. Reviewing Functions
    2. ANSI C Function Prototyping
    3. Recursion
    4. All C Functions Are Created Equal
    5. Compiling Programs with Two or More Source Code Files
    6. Finding Addresses: The & Operator
    7. Altering Variables in the Calling Function
    8. Pointers: A First Look
    9. Key Concepts
    10. Summary
    11. Review Questions
    12. Programming Exercises
  15. Arrays and Pointers
    1. Arrays
    2. Multidimensional Arrays
    3. Pointers and Arrays
    4. Functions, Arrays, and Pointers
    5. Pointer Operations
    6. Protecting Array Contents
    7. Pointers and Multidimensional Arrays
    8. Variable-Length Arrays (VLAs)
    9. Compound Literals
    10. Key Concepts
    11. Summary
    12. Review Questions
    13. Programming Exercises
  16. Character Strings and String Functions
    1. Defining Strings Within a Program
    2. Character String Arrays and Initialization
    3. String Input
    4. String Output
    5. The Do-It-Yourself Option
    6. String Functions
    7. A String Example: Sorting Strings
    8. The ctype.h Character Functions and Strings
    9. Command-Line Arguments
    10. String to Number Conversions
    11. Key Concepts
    12. Summary
    13. Review Questions
    14. Programming Exercises
  17. Storage Classes, Linkage, and Memory Management
    1. Storage Classes
    2. A Random Number Function and a Static Variable
    3. Roll 'Em
    4. ANSI C Type Qualifiers
    5. New Places for Old Keywords
    6. Key Concepts
    7. Summary
    8. Review Questions
    9. Programming Exercises
  18. File Input/Output
    1. Communicating with Files
    2. Standard I/O
    3. A Simple-Minded File-Condensing Program
    4. File I/O: fprintf(), fscanf(), fgets(), and fputs()
    5. Adventures in Random Access: fseek() and ftell()
    6. Behind the Scenes with Standard I/O
    7. Other Standard I/O Functions
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  19. Structures and Other Data Forms
    1. Sample Problem: Creating an Inventory of Books
    2. Setting Up the Structure Declaration
    3. Defining a Structure Variable
    4. Gaining Access to Structure Members
    5. Arrays of Structures
    6. Nested Structures
    7. Pointers to Structures
    8. Telling Functions About Structures
    9. Saving the Structure Contents in a File
    10. Structures: What Next?
    11. Unions: A Quick Look
    12. Enumerated Types
    13. typedef: A Quick Look
    14. Fancy Declarations
    15. Functions and Pointers
    16. Key Concepts
    17. Summary
    18. Review Questions
    19. Programming Exercises
  20. Bit Fiddling
    1. Binary Numbers, Bits, and Bytes
    2. Other Bases
    3. C's Bitwise Operators
    4. Bit Fields
    5. Key Concepts
    6. Summary
    7. Review Questions
    8. Programming Exercises
  21. The C Preprocessor and the C Library
    1. First Steps
    2. Manifest Constants: #define
    3. Using Arguments with #define
    4. Macro or Function?
    5. File Inclusion: #include
    6. Other Directives
    7. Inline Functions
    8. The C Library
    9. The Math Library
    10. The General Utilities Library
    11. The Assert Library
    12. memcpy() and memmove() from the string.h Library
    13. Variable Arguments: stdarg.h
    14. Key Concepts
    15. Summary
    16. Review Questions
    17. Programming Exercises
  22. Advanced Data Representation
    1. Exploring Data Representation
    2. Beyond the Array to the Linked List
    3. Abstract Data Types (ADTs)
    4. Getting Queued with an ADT
    5. Simulating with a Queue
    6. The Linked List Versus the Array
    7. Binary Search Trees
    8. Other Directions
    9. Key Concepts
    10. Summary
    11. Review Questions
    12. Programming Exercises
  23. Answers to the Review Questions
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
  24. Reference Section
    1. Section I—Additional Reading
    2. Section II—C Operators
    3. Logical Operators
    4. Sign Operators
    5. Section III—Basic Types and Storage Classes
    6. Summary: Qualifiers
    7. Section IV—Expressions, Statements, and Program Flow
    8. Section V—The Standard ANSI C Library with C99 Additions
    9. Math Library: math.h
    10. Variable Arguments: stdarg.h
    11. Standard I/O Library: stdio.h
    12. General Utilities: stdlib.h
    13. Section VI—Extended Integer Types
    14. Section VII—Expanded Character Support
    15. Section VIII—C99 Numeric Computational Enhancements
    16. Section IX—Differences Between C and C++
  25. Index