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

Book Description

C Primer Plus is a carefully tested, well-crafted, and complete tutorial on a subject core to programmers and developers. This computer science classic teaches principles of programming, including structured code and top-down design.

Author and educator Stephen Prata has created an introduction to C that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.

Review questions and programming exercises at the end of each chapter bring out the most critical pieces of information and help readers understand and digest the most difficult concepts. A friendly and easy-to-use self-study guide, this book is 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.

The sixth edition of this book has been updated and expanded to cover the latest developments in C as well as to take a detailed look at the new C11 standard. In C Primer Plus you’ll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:

  • Complete, integrated discussion of both C language fundamentals and additional features

  • Clear guidance about when and why to use different parts of the language

  • Hands-on learning with concise and simple examples that develop your understanding of a concept or two at a time

  • Hundreds of practical sample programs

  • Review questions and programming exercises at the end of each chapter to test your understanding

  • Coverage of generic C to give you the greatest flexibility

  • Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Developer’s Library
    5. Contents at a Glance
    6. Table of Contents
    7. Dedication
    8. About the Author
    9. Acknowledgments
    10. We Want to Hear from You!
    11. Reader Services
    12. Preface
      1. Approach and Goals
    13. 1. Getting Ready
      1. Whence C?
      2. Why C?
      3. Whither C?
      4. What Computers Do
      5. High-level Computer Languages and Compilers
      6. Language Standards
      7. Using C: Seven Steps
      8. Programming Mechanics
      9. How This Book Is Organized
      10. Conventions Used in This Book
      11. Summary
      12. Review Questions
      13. Programming Exercise
    14. 2. Introducing C
      1. A Simple Example of C
      2. The Example Explained
      3. The Structure of a Simple Program
      4. Tips on Making Your Programs Readable
      5. Taking Another Step in Using C
      6. While You’re at It—Multiple Functions
      7. Introducing Debugging
      8. Keywords and Reserved Identifiers
      9. Key Concepts
      10. Summary
      11. Review Questions
      12. Programming Exercises
    15. 3. Data and C
      1. A Sample Program
      2. Data Variables and Constants
      3. Data: Data-Type Keywords
      4. Basic C Data Types
      5. Using Data Types
      6. Arguments and Pitfalls
      7. One More Example: Escape Sequences
      8. Key Concepts
      9. Summary
      10. Review Questions
      11. Programming Exercises
    16. 4. 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. Key Concepts
      6. Summary
      7. Review Questions
      8. Programming Exercises
    17. 5. 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
    18. 6. C Control Statements: Looping
      1. Revisiting the while Loop
      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. Introducing Arrays
      12. A Loop Example Using a Function Return Value
      13. Key Concepts
      14. Summary
      15. Review Questions
      16. Programming Exercises
    19. 7. 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
    20. 8. 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
    21. 9. Functions
      1. Reviewing Functions
      2. ANSI C Function Prototyping
      3. Recursion
      4. Compiling Programs with Two or More Source Code Files
      5. Finding Addresses: The & Operator
      6. Altering Variables in the Calling Function
      7. Pointers: A First Look
      8. Key Concepts
      9. Summary
      10. Review Questions
      11. Programming Exercises
    22. 10. 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
    23. 11. Character Strings and String Functions
      1. Representing Strings and String I/O
      2. String Input
      3. String Output
      4. The Do-It-Yourself Option
      5. String Functions
      6. A String Example: Sorting Strings
      7. The ctype.h Character Functions and Strings
      8. Command-Line Arguments
      9. String-to-Number Conversions
      10. Key Concepts
      11. Summary
      12. Review Questions
      13. Programming Exercises
    24. 12. Storage Classes, Linkage, and Memory Management
      1. Storage Classes
      2. A Random-Number Function and a Static Variable
      3. Roll ’Em
      4. Allocated Memory: malloc() and free()
      5. ANSI C Type Qualifiers
      6. Key Concepts
      7. Summary
      8. Review Questions
      9. Programming Exercises
    25. 13. 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
    26. 14. 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. Arrays of Structures
      5. Nested Structures
      6. Pointers to Structures
      7. Telling Functions About Structures
      8. Saving the Structure Contents in a File
      9. Structures: What Next?
      10. Unions: A Quick Look
      11. Enumerated Types
      12. typedef: A Quick Look
      13. Fancy Declarations
      14. Functions and Pointers
      15. Key Concepts
      16. Summary
      17. Review Questions
      18. Programming Exercises
    27. 15. Bit Fiddling
      1. Binary Numbers, Bits, and Bytes
      2. Other Number Bases
      3. C’s Bitwise Operators
      4. Bit Fields
      5. Alignment Features (C11)
      6. Key Concepts
      7. Summary
      8. Review Questions
      9. Programming Exercises
    28. 16. The C Preprocessor and the C Library
      1. First Steps in Translating a Program
      2. Manifest Constants: #define
      3. Using Arguments with #define
      4. Macro or Function?
      5. File Inclusion: #include
      6. Other Directives
      7. Inline Functions (C99)
      8. _Noreturn Functions (C11)
      9. The C Library
      10. The Math Library
      11. The General Utilities Library
      12. The Assert Library
      13. memcpy() and memmove() from the string.h Library
      14. Variable Arguments: stdarg.h
      15. Key Concepts
      16. Summary
      17. Review Questions
      18. Programming Exercises
    29. 17. 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
    30. Appendixes
      1. A. Answers to the Review Questions
        1. Answers to Review Questions for Chapter 1
        2. Answers to Review Questions for Chapter 2
        3. Answers to Review Questions for Chapter 3
        4. Answers to Review Questions for Chapter 4
        5. Answers to Review Questions for Chapter 5
        6. Answers to Review Questions for Chapter 6
        7. Answers to Review Questions for Chapter 7
        8. Answers to Review Questions for Chapter 8
        9. Answers to Review Questions for Chapter 9
        10. Answers to Review Questions for Chapter 10
        11. Answers to Review Questions for Chapter 11
        12. Answers to Review Questions for Chapter 12
        13. Answers to Review Questions for Chapter 13
        14. Answers to Review Questions for Chapter 14
        15. Answers to Review Questions for Chapter 15
        16. Answers to Review Questions for Chapter 16
        17. Answers to Review Questions for Chapter 17
      2. B. Reference Section
        1. Section I: Additional Reading
        2. Section II: C Operators
        3. Section III: Basic Types and Storage Classes
        4. Section IV: Expressions, Statements, and Program Flow
        5. Section V: The Standard ANSI C Library with C99 and C11 Additions
        6. Section VI: Extended Integer Types
        7. Section VII: Expanded Character Support
        8. Section VIII: C99/C11 Numeric Computational Enhancements
        9. Section IX: Differences Between C and C++
    31. Index