You are previewing C: From Theory to Practice.
O'Reilly logo
C: From Theory to Practice

Book Description

Designed for a compulsory fundamental course, C: From Theory to Practice uses a hands-on approach to teach the C programming language, using numerous examples and a clear, concise presentation. Easy to use and classroom tested, this textbook includes more than 500 exercises and examples of progressive difficulty to help students in understanding all the aspects and peculiarities of C. The exercises test students on various levels of programming and the examples enhance their concrete understanding of programming know-how. Divided into three parts, this book:

  • Introduces the basic concepts of C, like getting input from a user, C’s operators, selection statements, and loops.
  • Emphasizes major features of C such as arrays, pointers, functions and strings.
  • Covers advanced topics such as like searching and sorting arrays’ algorithms, structures and unions, memory management, the preprocessor and files.

The book tests the skills of beginners and advanced developers by providing an easy-to-read compilation of the C theory enriched with tips and advice as well as difficulty-scaled solved programming exercises. It decodes the secrets of the C language, providing inside information and programming knowledge through practical examples and meaningful advice. The examples are designed to be short, concrete, and substantial, quickly giving students the know-how they need.

Table of Contents

  1. Preface
  2. Acknowledgments
  3. Authors
  4. Chapter 1 - Introduction to C
    1. History of C
    2. ANSI Standard
    3. Advantages of C
    4. Disadvantages of C
    5. C Program Life Cycle
    6. Write a C Program
    7. First C Program
      1. #include Directive
      2. main() Function
      3. Add Comments
    8. Compilation
      1. Common Errors
    9. Linking
    10. Run the Program
    11. Reference
  5. Chapter 2 - Data Types, Variables, and Data Output
    1. Variables
    2. Rules for Naming Variables
    3. Variable Name Conventions
    4. Declaring Variables
    5. Assigning Values to Variables
    6. Constants
    7. #define Directive
    8. printf() Function
      1. Escape Sequences
      2. Conversion Specifications
      3. Return Value
      4. Printing Variables
      5. Optional Fields
        1. Precision
        2. Field Width
        3. Prefix
        4. Flags
    9. Type Casting
    10. Exercises
    11. Unsolved Exercises
  6. Chapter 3 - Getting Input with scanf()
    1. scanf() Function
      1. scanf() Examples
      2. Use of Ordinary Characters
      3. scanf() Return Value
    2. Exercises
    3. Unsolved Exercises
  7. Chapter 4 - Operators
    1. Assignment Operator
    2. Arithmetic Operators
    3. Increment and Decrement Operators
    4. Relational Operators
    5. Exercises
    6. Not Operator !
    7. Exercises
    8. Compound Operators
    9. Exercise
    10. Logical Operators
      1. Operator &&
      2. Operator ||
    11. Exercises
    12. Comma Operator
    13. Exercise
    14. sizeof Operator
    15. enum Type
    16. Bitwise Operators
      1. & Operator
      2. | Operator
      3. ˄ Operator
      4. ∼ Operator
    17. Exercise
    18. Shift Operators
      1. >> Operator
      2. << Operator
    19. Exercises
    20. Operator Precedence
    21. Unsolved Exercises
  8. Chapter 5 - Program Control
    1. if Statement
      1. Common Errors
    2. if-else Statement
    3. Nested if Statements
    4. Exercises
    5. Conditional Operator ?:
    6. Exercises
    7. switch Statement
      1. switch versus if
    8. Exercises
    9. Unsolved Exercises
  9. Chapter 6 - Loops
    1. for Statement
      1. Omitting Expressions
    2. Exercises
    3. break Statement
    4. continue Statement
    5. Exercises
    6. Nested Loops
    7. Exercises
    8. while Statement
    9. Exercises
    10. do-while Statement
    11. Exercises
    12. goto Statement
    13. Unsolved Exercises
  10. Chapter 7 - Arrays
    1. Declaring Arrays
    2. Accessing Array Elements
    3. Array Initialization
    4. Exercises
    5. Two-Dimensional Arrays
      1. Two-Dimensional Array Declaration
      2. Accessing the Elements of a Two-Dimensional Array
      3. Two-Dimensional Array Initialization
    6. Exercises
    7. Unsolved Exercises
  11. Chapter 8 - Pointers
    1. Pointers and Memory
    2. Declaring Pointers
    3. Pointer Initialization
    4. NULL Value
    5. Use a Pointer
    6. Exercises
    7. void* Pointer
    8. Use of const Keyword
    9. Pointer Arithmetic
      1. Pointers and Integers
      2. Subtracting Pointers
      3. Comparing Pointers
    10. Exercises
    11. Pointers and Arrays
    12. Exercises
    13. Arrays of Pointers
    14. Exercises
    15. Pointer to Pointer
    16. Exercises
    17. Pointers and Two-Dimensional Arrays
    18. Exercises
    19. Pointer to Function
    20. Exercise
    21. Array of Pointers to Functions
    22. Unsolved Exercises
  12. Chapter 9 - Characters
    1. char Type
    2. Exercises
    3. getchar() Function
    4. Exercises
  13. Chapter 10 - Strings
    1. String Literals
    2. Storing Strings
    3. Exercises
    4. Writing Strings
    5. Exercise
    6. Pointers and String Literals
    7. Exercises
    8. Read Strings
    9. For a Safe Reading of Strings
    10. Exercises
    11. String Functions
      1. strlen() Function
    12. Exercises
      1. strcpy() Function
    13. Exercises
      1. strncpy() Function
      2. strcat() Function
      3. strcmp() and strncmp() Functions
    14. Exercises
    15. Two-Dimensional Arrays and Strings
    16. Exercises
    17. Unsolved Exercises
  14. Chapter 11 - Functions
    1. Function Declaration
    2. Return Type
    3. Function Parameters
    4. Function Definition
    5. return Statement
    6. Function Call
      1. Function Call without Parameters
      2. Function Call with Parameters
      3. Passing Values
    7. Exercises
    8. Variables Scope
      1. Local Variables
      2. Global Variables
      3. Static Variables
    9. Arrays as Arguments
    10. Exercises
    11. Function Call with Parameter Two-Dimensional Array
    12. Exercises
    13. Passing Data in main() Function
    14. Exercises
    15. Functions with Variable Number of Parameters
    16. Recursive Functions
    17. Exercises
    18. Unsolved Exercises
  15. Chapter 12 - Searching and Sorting Arrays
    1. Searching Arrays
      1. Linear Search
    2. Exercises
      1. Binary Search
    3. Exercises
    4. Sorting Arrays
      1. Selection Sort
    5. Exercises
      1. Insertion Sort
    6. Exercise
      1. Bubble Sort
    7. Exercise
    8. bsearch() and qsort() Library Functions
    9. Exercise
  16. Chapter 13 - Structures and Unions
    1. Structures
      1. Defining a Structure
      2. Declaring Structure Variables
      3. Accessing the Structure Fields
      4. Pointer to a Structure Field
      5. Structure Operations
      6. Structures Containing Arrays
      7. Structures Containing Pointers
      8. Structures Containing Structures
    2. Exercise
      1. Bit Fields
      2. Pointer to Structure
      3. Arrays of Structures
    3. Exercises
      1. Structures as Function Arguments
    4. Exercises
    5. Unions
      1. Using Unions
      2. Access Union Fields
    6. Exercise
    7. Unsolved Exercises
  17. Chapter 14 - Memory Management and Data Structures
    1. Memory Blocks
    2. Static Memory Allocation
    3. Dynamic Memory Allocation
      1. malloc() Function
      2. free() Function
    4. memcpy() and memmove() Functions
    5. memcmp() Function
    6. Exercises
    7. Dynamic Data Structures
      1. Linked List
        1. Insert a Node
        2. Delete a Node
    8. Examples
      1. Implementing a Stack
    9. Exercise
      1. Implementing a Queue
    10. Exercise
      1. Implementing a Linked List
    11. Exercises
    12. Unsolved Exercises
  18. Chapter 15 - Files
    1. Files in C
    2. Open a File
    3. Close a File
    4. Process a File
    5. Write Data in a Text File
      1. fputs() Function
      2. fprintf() Function
    6. Exercise
      1. fputc() Function
    7. Exercises
    8. Read Data from a Text File
      1. fscanf() Function
    9. Exercises
      1. fgets() Function
    10. Exercise
      1. fgetc() Function
    11. Exercises
    12. End of File
    13. fseek() Function
    14. ftell() Function
    15. Write and Read Data from a Binary File
      1. fwrite() Function
      2. fread() Function
    16. Exercises
    17. Exercises
    18. feof() Function
    19. Exercise
    20. Unsolved Exercises
  19. Chapter 16 - Preprocessor Directives and Macros
    1. Simple Macros
    2. Macros with Parameters
    3. # and ## Preprocessor Operators
    4. Preprocessor Directives and Conditional Compilation
      1. #if, #else, #elif, and #endif Directives
      2. #ifdef, #ifndef, and #undef Directives
      3. defined Operator
    5. Exercises
    6. Unsolved Exercises
  20. Chapter 17 - Review Exercises
  21. Appendix A
    1. Precedence Table
  22. Appendix B
    1. ASCII Tables
  23. Appendix C
    1. Library Functions
  24. Appendix D
    1. Hexadecimal System
  25. Bibliography