You are previewing Programming in C, Third Edition.
O'Reilly logo
Programming in C, Third Edition

Book Description

Learn the C programming language from one of the best. Stephen Kochan's Programming in C is thorough with easy-to-follow instructions that are sure to benefit beginning programmers. This book provides readers with practical examples of how the C programming language can be used with small, fast programs, similar to the programming used by large game developers such as Nintendo. If you want a one-stop-source for C programming, this book is it.The book is appropriate for all introductory-to-intermediate courses on programming in the C language, including courses covering C programming for games and small-device platforms.

Programming in C, Third Edition is a thoroughly revised and updated edition of Steven Kochan's classic C programming tutorial: a book that has helped thousands of students master C over the past twenty years. This edition fully reflects the latest C standard and contains current source code. It has been crafted to help students master C regardless of the platform they intend to use or the applications they intend to create -- including small-device and gaming applications, where C's elegance and speed make it especially valuable.

Kochan begins with the fundamentals, then covers every facet of C language programming: variables, data types, arithmetic expressions, program looping, making decisions, arrays, functions, structures, character strings, pointers, operations on bits, the preprocessors, I/O, and more. Coverage also includes chapters on working with larger programs; debugging programs; and the fundamentals of object-oriented programming. Appendices include a complete language summary, an introduction to the Standard C Library, coverage of compiling and running programs using gcc, common programming mistakes, and more.

Table of Contents

  1. Copyright
    1. Dedication
  2. Developer’s Library
  3. Preface
  4. About the Author
  5. Acknowledgements
  6. We Want to Hear from You
  7. 1. Introduction
  8. 2. Some Fundamentals
    1. Programming
    2. Higher-Level Languages
    3. Operating Systems
    4. Compiling Programs
    5. Integrated Development Environments
    6. Language Interpreters
  9. 3. Compiling and Running Your First Program
    1. Compiling Your Program
    2. Running Your Program
    3. Understanding Your First Program
    4. Displaying the Values of Variables
    5. Comments
    6. Exercises
  10. 4. Variables, Data Types, and Arithmetic Expressions
    1. Working with Variables
    2. Understanding Data Types and Constants
      1. The Basic Integer Type int
        1. Storage Sizes and Ranges
      2. The Floating Number Type float
      3. The Extended Precision Type double
      4. The Single Character Type char
      5. The Boolean Data Type _Bool
      6. Type Specifiers: long, long long, short, unsigned, and signed
    3. Working with Arithmetic Expressions
      1. Integer Arithmetic and the Unary Minus Operator
      2. The Modulus Operator
      3. Integer and Floating-Point Conversions
        1. The Type Cast Operator
    4. Combining Operations with Assignment: The Assignment Operators
    5. Types _Complex and _Imaginary
    6. Exercises
  11. 5. Program Looping
    1. The for Statement
      1. Relational Operators
      2. Aligning Output
      3. Program Input
      4. Nested for Loops
      5. for Loop Variants
        1. Multiple Expressions
        2. Omitting Fields
        3. Declaring Variables
    2. The while Statement
    3. The do Statement
      1. The break Statement
      2. The continue Statement
    4. Exercises
  12. 6. Making Decisions
    1. The if Statement
      1. The if-else Construct
      2. Compound Relational Tests
      3. Nested if Statements
      4. The else if Construct
    2. The switch Statement
    3. Boolean Variables
    4. The Conditional Operator
    5. Exercises
  13. 7. Working with Arrays
    1. Defining an Array
      1. Using Array Elements as Counters
      2. Generating Fibonacci Numbers
      3. Using an Array to Generate Prime Numbers
    2. Initializing Arrays
    3. Character Arrays
      1. Base Conversion Using Arrays
      2. The const Qualifier
    4. Multidimensional Arrays
    5. Variable-Length Arrays
    6. Exercises
  14. 8. Working with Functions
    1. Defining a Function
    2. Arguments and Local Variables
      1. Function Prototype Declaration
      2. Automatic Local Variables
    3. Returning Function Results
    4. Functions Calling Functions Calling...
      1. Declaring Return Types and Argument Types
      2. Checking Function Arguments
    5. Top-Down Programming
    6. Functions and Arrays
      1. Assignment Operators
      2. Sorting Arrays
      3. Multidimensional Arrays
        1. Multidimensional Variable-Length Arrays and Functions
    7. Global Variables
    8. Automatic and Static Variables
    9. Recursive Functions
    10. Exercises
  15. 9. Working with Structures
    1. A Structure for Storing the Date
      1. Using Structures in Expressions
    2. Functions and Structures
      1. A Structure for Storing the Time
    3. Initializing Structures
      1. Compound Literals
    4. Arrays of Structures
    5. Structures Containing Structures
    6. Structures Containing Arrays
    7. Structure Variants
    8. Exercises
  16. 10. Character Strings
    1. Arrays of Characters
    2. Variable-Length Character Strings
      1. Initializing and Displaying Character Strings
      2. Testing Two Character Strings for Equality
      3. Inputting Character Strings
      4. Single-Character Input
      5. The Null String
    3. Escape Characters
    4. More on Constant Strings
    5. Character Strings, Structures, and Arrays
      1. A Better Search Method
        1. Binary Search Algorithm
    6. Character Operations
    7. Exercises
  17. 11. Pointers
    1. Defining a Pointer Variable
    2. Using Pointers in Expressions
    3. Working with Pointers and Structures
      1. Structures Containing Pointers
      2. Linked Lists
    4. The Keyword const and Pointers
    5. Pointers and Functions
    6. Pointers and Arrays
      1. A Slight Digression About Program Optimization
      2. Is It an Array or Is It a Pointer?
      3. Pointers to Character Strings
      4. Constant Character Strings and Pointers
      5. The Increment and Decrement Operators Revisited
    7. Operations on Pointers
    8. Pointers to Functions
    9. Pointers and Memory Addresses
    10. Exercises
  18. 12. Operations on Bits
    1. Bit Operators
      1. The Bitwise AND Operator
      2. The Bitwise Inclusive-OR Operator
      3. The Bitwise Exclusive-OR Operator
      4. The Ones Complement Operator
      5. The Left Shift Operator
      6. The Right Shift Operator
      7. A Shift Function
      8. Rotating Bits
    2. Bit Fields
    3. Exercises
  19. 13. The Preprocessor
    1. The #define Statement
      1. Program Extendability
      2. Program Portability
      3. More Advanced Types of Definitions
        1. Arguments and Macros
        2. Variable Number of Arguments to Macros
      4. The # Operator
      5. The ## Operator
    2. The #include Statement
      1. System Include Files
    3. Conditional Compilation
      1. The #ifdef, #endif, #else, and #ifndef Statements
        1. Avoiding Multiple Inclusion of Header Files
      2. The #if and #elif Preprocessor Statements
      3. The #undef Statement
    4. Exercises
  20. 14. More on Data Types
    1. Enumerated Data Types
    2. The typedef Statement
    3. Data Type Conversions
      1. Sign Extension
      2. Argument Conversion
    4. Exercises
  21. 15. Working with Larger Programs
    1. Dividing Your Program into Multiple Files
      1. Compiling Multiple Source Files from the Command Line
    2. Communication Between Modules
      1. External Variables
      2. Static Versus Extern Variables and Functions
      3. Using Header Files Effectively
    3. Other Utilities for Working with Larger Programs
      1. The make Utility
      2. The cvs Utility
      3. Unix Utilities: ar, grep, sed, and so on
  22. 16. Input and Output Operations in C
    1. Character I/O: getchar and putchar
    2. Formatted I/O: printf and scanf
      1. The printf Function
      2. The scanf Function
    3. Input and Output Operations with Files
      1. Redirecting I/O to a File
      2. End of File
    4. Special Functions for Working with Files
      1. The fopen Function
      2. The getc and putc Functions
      3. The fclose Function
      4. The feof Function
      5. The fprintf and fscanf Functions
      6. The fgets and fputs Functions
      7. stdin, stdout, and stderr
      8. The exit Function
      9. Renaming and Removing Files
    5. Exercises
  23. 17. Miscellaneous and Advanced Features
    1. Miscellaneous Language Statements
      1. The goto Statement
      2. The null Statement
    2. Working with Unions
    3. The Comma Operator
    4. Type Qualifiers
      1. The register Qualifier
      2. The volatile Qualifier
      3. The restrict Qualifier
    5. Command-Line Arguments
    6. Dynamic Memory Allocation
      1. The calloc and malloc Functions
      2. The sizeof Operator
      3. The free Function
  24. 18. Debugging Programs
    1. Debugging with the Preprocessor
    2. Debugging Programs with gdb
      1. Working with Variables
      2. Source File Display
      3. Controlling Program Execution
        1. Inserting Breakpoints
        2. Single Stepping
        3. Listing and Deleting Breakpoints
      4. Getting a Stack Trace
      5. Calling Functions and Setting Arrays and Structures
      6. Getting Help with gdb Commands
      7. Odds and Ends
  25. 19. Object-Oriented Programming
    1. What Is an Object Anyway?
    2. Instances and Methods
    3. Writing a C Program to Work with Fractions
    4. Defining an Objective-C Class to Work with Fractions
    5. Defining a C++ Class to Work with Fractions
    6. Defining a C# Class to Work with Fractions
  26. A. C Language Summary
    1. 1.0. Digraphs and Identifiers
      1. 1.1. Digraph Characters
      2. 1.2. Identifiers
        1. 1.2.1. Universal Character Names
        2. 1.2.2. Keywords
    2. 2.0. Comments
    3. 3.0. Constants
      1. 3.1. Integer Constants
      2. 3.2. Floating-Point Constants
      3. 3.3. Character Constants
        1. 3.3.1. Escape Sequences
        2. 3.3.2. Wide Character Constants
      4. 3.4. Character String Constants
        1. 3.4.1. Character String Concatenation
        2. 3.4.2. Multibyte Characters
        3. 3.4.3. Wide Character String Constants
      5. 3.5. Enumeration Constants
    4. 4.0. Data Types and Declarations
      1. 4.1. Declarations
      2. 4.2. Basic Data Types
      3. 4.3. Derived Data Types
        1. 4.3.1. Arrays
          1. Single-Dimensional Arrays
          2. 4.3.1.1. Variable-Length Arrays
          3. 4.3.1.2. Multidimensional Arrays
        2. 4.3.2. Structures
        3. 4.3.3. Unions
        4. 4.3.4. Pointers
      4. 4.4. Enumerated Data Types
      5. 4.5. The typedef Statement
      6. 4.6. Type Modifiers const, volatile, and restrict
    5. 5.0. Expressions
      1. 5.1. Summary of C Operators
      2. 5.2. Constant Expressions
      3. 5.3. Arithmetic Operators
      4. 5.4. Logical Operators
      5. 5.5. Relational Operators
      6. 5.6. Bitwise Operators
      7. 5.7. Increment and Decrement Operators
      8. 5.8. Assignment Operators
      9. 5.9. Conditional Operators
      10. 5.10. Type Cast Operator
      11. 5.11. sizeof Operator
      12. 5.12. Comma Operator
      13. 5.13. Basic Operations with Arrays
      14. 5.14. Basic Operations with Structures
      15. 5.15. Basic Operations with Pointers
        1. Pointers to Arrays
        2. Pointers to Structures
      16. 5.16. Compound Literals
      17. 5.17. Conversion of Basic Data Types
    6. 6.0. Storage Classes and Scope
      1. 6.1. Functions
      2. 6.2. Variables
    7. 7.0. Functions
      1. 7.1. Function Definition
      2. 7.2. Function Call
      3. 7.3. Function Pointers
    8. 8.0. Statements
      1. 8.1. Compound Statements
      2. 8.2. The break Statement
      3. 8.3. The continue Statement
      4. 8.4. The do Statement
      5. 8.5. The for Statement
      6. 8.6. The goto Statement
      7. 8.7. The if Statement
      8. 8.8. The null Statement
      9. 8.9. The return Statement
      10. 8.10. The switch Statement
      11. 8.11. The while Statement
    9. 9.0. The Preprocessor
      1. 9.1. Trigraph Sequences
      2. 9.2. Preprocessor Directives
        1. 9.2.1. The #define Directive
        2. 9.2.2. The #error Directive
        3. 9.2.3. The #if Directive
        4. 9.2.4. The #ifdef Directive
        5. 9.2.5. The #ifndef Directive
        6. 9.2.6. The #include Directive
        7. 9.2.7. The #line Directive
        8. 9.2.8. The #pragma Directive
        9. 9.2.9. The #undef Directive
        10. 9.2.10. The # Directive
      3. 9.3. Predefined Identifiers
  27. B. The Standard C Library
    1. Standard Header Files
      1. <stddef.h>
      2. <limits.h>
      3. <stdbool.h>
      4. <float.h>
      5. <stdint.h>
    2. String Functions
    3. Memory Functions
    4. Character Functions
    5. I/O Functions
    6. In-Memory Format Conversion Functions
    7. String-to-Number Conversion
    8. Dynamic Memory Allocation Functions
    9. Math Functions
      1. Complex Arithmetic
    10. General Utility Functions
  28. C. Compiling Programs with gcc
    1. General Command Format
    2. Command-Line Options
  29. D. Common Programming Mistakes
  30. E. Resources
    1. Answers to Exercises, Errata, etc.
    2. The C Programming Language
      1. Books
      2. Web Sites
      3. Newsgroups
    3. C Compilers and Integrated Development Environments
      1. gcc
      2. MinGW
      3. CygWin
      4. Visual Studio
      5. CodeWarrior
      6. Kylix
    4. Miscellaneous
      1. Object-Oriented Programming
      2. The C++ Language
      3. The C# Language
      4. The Objective-C Language
      5. Development Tools