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. Dedication
  7. About the Author
  8. Acknowledgments
  9. We Want to Hear from You!
  10. Reader Services
  11. Preface
  12. 1 Getting Ready
    1. Whence C?
    2. Why C?
      1. Design Features
      2. Efficiency
      3. Portability
      4. Power and Flexibility
      5. Programmer Oriented
      6. Shortcomings
    3. Whither C?
    4. What Computers Do
    5. High-level Computer Languages and Compilers
    6. Language Standards
      1. The First ANSI/ISO C Standard
      2. The C99 Standard
      3. The C11 Standard
    7. Using C: Seven Steps
      1. Step 1: Define the Program Objectives
      2. Step 2: Design the Program
      3. Step 3: Write the Code
      4. Step 4: Compile
      5. Step 5: Run the Program
      6. Step 6: Test and Debug the Program
      7. Step 7: Maintain and Modify the Program
      8. Commentary
    8. Programming Mechanics
      1. Object Code Files, Executable Files, and Libraries
      2. Unix System
      3. The GNU Compiler Collection and the LLVM Project
      4. Linux Systems
      5. Command-Line Compilers for the PC
      6. Integrated Development Environments (Windows)
      7. The Windows/Linux Option
      8. C on the Macintosh
    9. How This Book Is Organized
    10. Conventions Used in This Book
      1. Typeface
      2. Program Output
      3. Special Elements
    11. Summary
    12. Review Questions
    13. Programming Exercise
  13. 2 Introducing C
    1. A Simple Example of C
    2. The Example Explained
      1. Pass 1: Quick Synopsis
      2. Pass 2: Program Details
    3. The Structure of a Simple Program
    4. Tips on Making Your Programs Readable
    5. Taking Another Step in Using C
      1. Documentation
      2. Multiple Declarations
      3. Multiplication
      4. Printing Multiple Values
    6. While You’re at It—Multiple Functions
    7. Introducing Debugging
      1. Syntax Errors
      2. Semantic Errors
      3. Program State
    8. Keywords and Reserved Identifiers
    9. Key Concepts
    10. Summary
    11. Review Questions
    12. Programming Exercises
  14. 3 Data and C
    1. A Sample Program
      1. What’s New in This Program?
    2. Data Variables and Constants
    3. Data: Data-Type Keywords
      1. Integer Versus Floating-Point Types
      2. The Integer
      3. The Floating-Point Number
    4. Basic C Data Types
      1. The int Type
      2. Other Integer Types
      3. Using Characters: Type char
      4. The _Bool Type
      5. Portable Types: stdint.h and inttypes.h
      6. Types float, double, and long double
      7. Complex and Imaginary Types
      8. Beyond the Basic Types
      9. Type Sizes
    5. Using Data Types
    6. Arguments and Pitfalls
    7. One More Example: Escape Sequences
      1. What Happens When the Program Runs
      2. Flushing the Output
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  15. 4 Character Strings and Formatted Input/Output
    1. Introductory Program
    2. Character Strings: An Introduction
      1. Type char Arrays and the Null Character
      2. Using Strings
      3. The strlen() Function
    3. Constants and the C Preprocessor
      1. The const Modifier
      2. Manifest Constants on the Job
    4. Exploring and Exploiting printf() and scanf()
      1. The printf() Function
      2. Using printf()
      3. Conversion Specification Modifiers for printf()
      4. What Does a Conversion Specification Convert?
      5. Using scanf()
      6. The * Modifier with printf() and scanf()
      7. Usage Tips for printf()
    5. Key Concepts
    6. Summary
    7. Review Questions
    8. Programming Exercises
  16. 5 Operators, Expressions, and Statements
    1. Introducing Loops
    2. Fundamental Operators
      1. Assignment Operator: =
      2. Addition Operator: +
      3. Subtraction Operator: –
      4. Sign Operators: – and +
      5. Multiplication Operator: *
      6. Division Operator: /
      7. Operator Precedence
      8. Precedence and the Order of Evaluation
    3. Some Additional Operators
      1. The sizeof Operator and the size_t Type
      2. Modulus Operator: %
      3. Increment and Decrement Operators: ++ and --
      4. Decrementing: --
      5. Precedence
      6. Don’t Be Too Clever
    4. Expressions and Statements
      1. Expressions
      2. Statements
      3. Compound Statements (Blocks)
    5. Type Conversions
      1. The Cast Operator
    6. Function with Arguments
    7. A Sample Program
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  17. 6 C Control Statements: Looping
    1. Revisiting the while Loop
      1. Program Comments
      2. C-Style Reading Loop
    2. The while Statement
      1. Terminating a while Loop
      2. When a Loop Terminates
      3. while: An Entry-Condition Loop
      4. Syntax Points
    3. Which Is Bigger: Using Relational Operators and Expressions
      1. What Is Truth?
      2. What Else Is True?
      3. Troubles with Truth
      4. The New _Bool Type
      5. Precedence of Relational Operators
    4. Indefinite Loops and Counting Loops
    5. The for Loop
      1. Using for for Flexibility
    6. More Assignment Operators: +=, -=, *=, /=, %=
    7. The Comma Operator
      1. Zeno Meets the for Loop
    8. An Exit-Condition Loop: do while
    9. Which Loop?
    10. Nested Loops
      1. Program Discussion
      2. A Nested Variation
    11. Introducing Arrays
      1. Using a for Loop with an Array
    12. A Loop Example Using a Function Return Value
      1. Program Discussion
      2. Using Functions with Return Values
    13. Key Concepts
    14. Summary
    15. Review Questions
    16. Programming Exercises
  18. 7 C Control Statements: Branching and Jumps
    1. The if Statement
    2. Adding else to the if Statement
      1. Another Example: Introducing getchar() and putchar()
      2. The ctype.h Family of Character Functions
      3. Multiple Choice else if
      4. Pairing else with if
      5. More Nested ifs
    3. Let’s Get Logical
      1. Alternate Spellings: The iso646.h Header File
      2. Precedence
      3. Order of Evaluation
      4. Ranges
    4. A Word-Count Program
    5. The Conditional Operator: ?:
    6. Loop Aids: continue and break
      1. The continue Statement
      2. The break Statement
    7. Multiple Choice: switch and break
      1. Using the switch Statement
      2. Reading Only the First Character of a Line
      3. Multiple Labels
      4. switch and if else
    8. The goto Statement
      1. Avoiding goto
    9. Key Concepts
    10. Summary
    11. Review Questions
    12. Programming Exercises
  19. 8 Character Input/Output and Input Validation
    1. Single-Character I/O: getchar() and putchar()
    2. Buffers
    3. Terminating Keyboard Input
      1. Files, Streams, and Keyboard Input
      2. The End of File
    4. Redirection and Files
      1. Unix, Linux, and Windows Command Prompt Redirection
    5. Creating a Friendlier User Interface
      1. Working with Buffered Input
      2. Mixing Numeric and Character Input
    6. Input Validation
      1. Analyzing the Program
      2. The Input Stream and Numbers
    7. Menu Browsing
      1. Tasks
      2. Toward a Smoother Execution
      3. Mixing Character and Numeric Input
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  20. 9 Functions
    1. Reviewing Functions
      1. Creating and Using a Simple Function
      2. Analyzing the Program
      3. Function Arguments
      4. Defining a Function with an Argument: Formal Parameters
      5. Prototyping a Function with Arguments
      6. Calling a Function with an Argument: Actual Arguments
      7. The Black-Box Viewpoint
      8. Returning a Value from a Function with return
      9. Function Types
    2. ANSI C Function Prototyping
      1. The Problem
      2. The ANSI C Solution
      3. No Arguments and Unspecified Arguments
      4. Hooray for Prototypes
    3. Recursion
      1. Recursion Revealed
      2. Recursion Fundamentals
      3. Tail Recursion
      4. Recursion and Reversal
      5. Recursion Pros and Cons
    4. Compiling Programs with Two or More Source Code Files
      1. Unix
      2. Linux
      3. DOS Command-Line Compilers
      4. Windows and Apple IDE Compilers
      5. Using Header Files
    5. Finding Addresses: The & Operator
    6. Altering Variables in the Calling Function
    7. Pointers: A First Look
      1. The Indirection Operator: *
      2. Declaring Pointers
      3. Using Pointers to Communicate Between Functions
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  21. 10 Arrays and Pointers
    1. Arrays
      1. Initialization
      2. Designated Initializers (C99)
      3. Assigning Array Values
      4. Array Bounds
      5. Specifying an Array Size
    2. Multidimensional Arrays
      1. Initializing a Two-Dimensional Array
      2. More Dimensions
    3. Pointers and Arrays
    4. Functions, Arrays, and Pointers
      1. Using Pointer Parameters
      2. Comment: Pointers and Arrays
    5. Pointer Operations
    6. Protecting Array Contents
      1. Using const with Formal Parameters
      2. More About const
    7. Pointers and Multidimensional Arrays
      1. Pointers to Multidimensional Arrays
      2. Pointer Compatibility
      3. Functions and Multidimensional Arrays
    8. Variable-Length Arrays (VLAs)
    9. Compound Literals
    10. Key Concepts
    11. Summary
    12. Review Questions
    13. Programming Exercises
  22. 11 Character Strings and String Functions
    1. Representing Strings and String I/O
      1. Defining Strings Within a Program
      2. Pointers and Strings
    2. String Input
      1. Creating Space
      2. The Unfortunate gets() Function
      3. The Alternatives to gets()
      4. The scanf() Function
    3. String Output
      1. The puts() Function
      2. The fputs() Function
      3. The printf() Function
    4. The Do-It-Yourself Option
    5. String Functions
      1. The strlen() Function
      2. The strcat() Function
      3. The strncat() Function
      4. The strcmp() Function
      5. The strcpy() and strncpy() Functions
      6. The sprintf() Function
      7. Other String Functions
    6. A String Example: Sorting Strings
      1. Sorting Pointers Instead of Strings
      2. The Selection Sort Algorithm
    7. The ctype.h Character Functions and Strings
    8. Command-Line Arguments
      1. Command-Line Arguments in Integrated Environments
      2. Command-Line Arguments with the Macintosh
    9. String-to-Number Conversions
    10. Key Concepts
    11. Summary
    12. Review Questions
    13. Programming Exercises
  23. 12 Storage Classes, Linkage, and Memory Management
    1. Storage Classes
      1. Scope
      2. Linkage
      3. Storage Duration
      4. Automatic Variables
      5. Register Variables
      6. Static Variables with Block Scope
      7. Static Variables with External Linkage
      8. Static Variables with Internal Linkage
      9. Multiple Files
      10. Storage-Class Specifier Roundup
      11. Storage Classes and Functions
      12. Which Storage Class?
    2. A Random-Number Function and a Static Variable
    3. Roll ’Em
    4. Allocated Memory: malloc() and free()
      1. The Importance of free()
      2. The calloc() Function
      3. Dynamic Memory Allocation and Variable-Length Arrays
      4. Storage Classes and Dynamic Memory Allocation
    5. ANSI C Type Qualifiers
      1. The const Type Qualifier
      2. The volatile Type Qualifier
      3. The restrict Type Qualifier
      4. The _Atomic Type Qualifier (C11)
      5. New Places for Old Keywords
    6. Key Concepts
    7. Summary
    8. Review Questions
    9. Programming Exercises
  24. 13 File Input/Output
    1. Communicating with Files
      1. What Is a File?
      2. The Text Mode and the Binary Mode
      3. Levels of I/O
      4. Standard Files
    2. Standard I/O
      1. Checking for Command-Line Arguments
      2. The fopen() Function
      3. The getc() and putc() Functions
      4. End-of-File
      5. The fclose() Function
      6. Pointers to the Standard Files
    3. A Simple-Minded File-Condensing Program
    4. File I/O: fprintf(), fscanf(), fgets(), and fputs()
      1. The fprintf() and fscanf() Functions
      2. The fgets() and fputs() Functions
    5. Adventures in Random Access: fseek() and ftell()
      1. How fseek() and ftell() Work
      2. Binary Versus Text Mode
      3. Portability
      4. The fgetpos() and fsetpos() Functions
    6. Behind the Scenes with Standard I/O
    7. Other Standard I/O Functions
      1. The int ungetc(int c, FILE *fp) Function
      2. The int fflush() Function
      3. The int setvbuf() Function
      4. Binary I/O: fread() and fwrite()
      5. The size_t fwrite() Function
      6. The size_t fread() Function
      7. The int feof(FILE *fp) and int ferror(FILE *fp) Functions
      8. An fread() and fwrite() Example
      9. Random Access with Binary I/O
    8. Key Concepts
    9. Summary
    10. Review Questions
    11. Programming Exercises
  25. 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
      1. Initializing a Structure
      2. Gaining Access to Structure Members
      3. Initializers for Structures
    4. Arrays of Structures
      1. Declaring an Array of Structures
      2. Identifying Members of an Array of Structures
      3. Program Discussion
    5. Nested Structures
    6. Pointers to Structures
      1. Declaring and Initializing a Structure Pointer
      2. Member Access by Pointer
    7. Telling Functions About Structures
      1. Passing Structure Members
      2. Using the Structure Address
      3. Passing a Structure as an Argument
      4. More on Structure Features
      5. Structures or Pointer to Structures?
      6. Character Arrays or Character Pointers in a Structure
      7. Structure, Pointers, and malloc()
      8. Compound Literals and Structures (C99)
      9. Flexible Array Members (C99)
      10. Anonymous Structures (C11)
      11. Functions Using an Array of Structures
    8. Saving the Structure Contents in a File
      1. A Structure-Saving Example
      2. Program Points
    9. Structures: What Next?
    10. Unions: A Quick Look
      1. Using Unions
      2. Anonymous Unions (C11)
    11. Enumerated Types
      1. enum Constants
      2. Default Values
      3. Assigned Values
      4. enum Usage
      5. Shared Namespaces
    12. typedef: A Quick Look
    13. Fancy Declarations
    14. Functions and Pointers
    15. Key Concepts
    16. Summary
    17. Review Questions
    18. Programming Exercises
  26. 15 Bit Fiddling
    1. Binary Numbers, Bits, and Bytes
      1. Binary Integers
      2. Signed Integers
      3. Binary Floating Point
    2. Other Number Bases
      1. Octal
      2. Hexadecimal
    3. C’s Bitwise Operators
      1. Bitwise Logical Operators
      2. Usage: Masks
      3. Usage: Turning Bits On (Setting Bits)
      4. Usage: Turning Bits Off (Clearing Bits)
      5. Usage: Toggling Bits
      6. Usage: Checking the Value of a Bit
      7. Bitwise Shift Operators
      8. Programming Example
      9. Another Example
    4. Bit Fields
      1. Bit-Field Example
      2. Bit Fields and Bitwise Operators
    5. Alignment Features (C11)
    6. Key Concepts
    7. Summary
    8. Review Questions
    9. Programming Exercises
  27. 16 The C Preprocessor and the C Library
    1. First Steps in Translating a Program
    2. Manifest Constants: #define
      1. Tokens
      2. Redefining Constants
    3. Using Arguments with #define
      1. Creating Strings from Macro Arguments: The # Operator
      2. Preprocessor Glue: The ## Operator
      3. Variadic Macros: ... and __VA_ARGS__
    4. Macro or Function?
    5. File Inclusion: #include
      1. Header Files: An Example
      2. Uses for Header Files
    6. Other Directives
      1. The #undef Directive
      2. Being Defined—The C Preprocessor Perspective
      3. Conditional Compilation
      4. Predefined Macros
      5. #line and #error
      6. #pragma
      7. Generic Selection (C11)
    7. Inline Functions (C99)
    8. _Noreturn Functions (C11)
    9. The C Library
      1. Gaining Access to the C Library
      2. Using the Library Descriptions
    10. The Math Library
      1. A Little Trigonometry
      2. Type Variants
      3. The tgmath.h Library (C99)
    11. The General Utilities Library
      1. The exit() and atexit() Functions
      2. The qsort() Function
    12. The Assert Library
      1. Using assert
      2. _Static_assert (C11)
    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
  28. 17 Advanced Data Representation
    1. Exploring Data Representation
    2. Beyond the Array to the Linked List
      1. Using a Linked List
      2. Afterthoughts
    3. Abstract Data Types (ADTs)
      1. Getting Abstract
      2. Building an Interface
      3. Using the Interface
      4. Implementing the Interface
    4. Getting Queued with an ADT
      1. Defining the Queue Abstract Data Type
      2. Defining an Interface
      3. Implementing the Interface Data Representation
      4. Testing the Queue
    5. Simulating with a Queue
    6. The Linked List Versus the Array
    7. Binary Search Trees
      1. A Binary Tree ADT
      2. The Binary Search Tree Interface
      3. The Binary Tree Implementation
      4. Trying the Tree
      5. Tree Thoughts
    8. Other Directions
    9. Key Concepts
    10. Summary
    11. Review Questions
    12. Programming Exercises
  29. 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
  30. B Reference Section
    1. Section I: Additional Reading
      1. Online Resources
      2. C Language Books
      3. Programming Books
      4. Reference Books
      5. C++ Books
    2. Section II: C Operators
      1. Arithmetic Operators
      2. Relational Operators
      3. Assignment Operators
      4. Logical Operators
      5. The Conditional Operator
      6. Pointer-Related Operators
      7. Sign Operators
      8. Structure and Union Operators
      9. Bitwise Operators
      10. Miscellaneous Operators
    3. Section III: Basic Types and Storage Classes
      1. Summary: The Basic Data Types
      2. Summary: How to Declare a Simple Variable
      3. Summary: Qualifiers
    4. Section IV: Expressions, Statements, and Program Flow
      1. Summary: Expressions and Statements
      2. Summary: The while Statement
      3. Summary: The for Statement
      4. Summary: The do while Statement
      5. Summary: Using if Statements for Making Choices
      6. Summary: Multiple Choice with switch
      7. Summary: Program Jumps
    5. Section V: The Standard ANSI C Library with C99 and C11 Additions
      1. Diagnostics: assert.h
      2. Complex Numbers: complex.h (C99)
      3. Character Handling: ctype.h
      4. Error Reporting: errno.h
      5. Floating-Point Environment: fenv.h (C99)
      6. Floating-point Characteristics: float.h
      7. Format Conversion of Integer Types: inttypes.h (C99)
      8. Alternative Spellings: iso646.h
      9. Localization: locale.h
      10. Math Library: math.h
      11. Non-Local Jumps: setjmp.h
      12. Signal Handling: signal.h
      13. Alignment: stdalign.h (C11)
      14. Variable Arguments: stdarg.h
      15. Atomics Support: stdatomic.h (C11)
      16. Boolean Support: stdbool.h (C99)
      17. Common Definitions: stddef.h
      18. Integer Types: stdint.h
      19. Standard I/O Library: stdio.h
      20. General Utilities: stdlib.h
      21. _Noreturn: stdnoreturn.h
      22. String Handling: string.h
      23. Type-Generic Math: tgmath.h (C99)
      24. Threads: threads.h (C11)
      25. Date and Time: time.h
      26. Unicode Utilities: uchar.h (C11)
      27. Extended Multibyte and Wide-Character Utilities: wchar.h (C99)
      28. Wide Character Classification and Mapping Utilities: wctype.h (C99)
    6. Section VI: Extended Integer Types
      1. Exact-Width Types
      2. Minimum-Width Types
      3. Fastest Minimum-Width Types
      4. Maximum-Width Types
      5. Integers That Can Hold Pointer Values
      6. Extended Integer Constants
    7. Section VII: Expanded Character Support
      1. Trigraph Sequences
      2. Digraphs
      3. Alternative Spellings: iso646.h
      4. Multibyte Characters
      5. Universal Character Names (UCNs)
      6. Wide Characters
      7. Wide Characters and Multibyte Characters
    8. Section VIII: C99/C11 Numeric Computational Enhancements
      1. The IEC Floating-Point Standard
      2. The fenv.h Header File
      3. The STDC FP_CONTRACT Pragma
      4. Additions to the math.h Library
      5. Support for Complex Numbers
    9. Section IX: Differences Between C and C++
      1. Function Prototypes
      2. char Constants
      3. The const Modifier
      4. Structures and Unions
      5. Enumerations
      6. Pointer-to-void
      7. Boolean Types
      8. Alternative Spellings
      9. Wide-Character Support
      10. Complex Types
      11. Inline Functions
      12. C99/11 Features Not Found in C++11
    10. Index

Product information

  • Title: C Primer Plus, Sixth Edition
  • Author(s): Stephen Prata
  • Release date: November 2013
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780133432398