Learn C Programming - Second Edition

Book description

Get started with writing simple programs in C while learning core programming concepts

Key Features

  • Learn essential C concepts such as variables, data structures, functions, loops, and pointers
  • Grasp the core programming aspects that form the base of many modern programming languages
  • Work with updated code samples and cover array declaration and initialization in detail in this new edition

Book Description

The foundation for many modern programming languages such as C++, C#, JavaScript, and Go, C is widely used as a system programming language as well as for embedded systems and high-performance computing. With this book, you'll be able to get up to speed with C in no time.

The book takes you through basic programming concepts and shows you how to implement them in the C programming language. Throughout the book, you'll create and run programs that demonstrate essential C concepts, such as program structure with functions, control structures such as loops and conditional statements, and complex data structures. As you make progress, you'll get to grips with in-code documentation, testing, and validation methods. This new edition expands upon the use of enumerations, arrays, and additional C features, and provides two working programs based on the code used in the book. What's more, this book uses the method of intentional failure, where you'll develop a working program and then purposely break it to see what happens, thereby learning how to recognize possible mistakes when they happen.

By the end of this C programming book, you'll have developed basic programming skills in C that can be easily applied to other programming languages and have gained a solid foundation for you to build on as a programmer.

What you will learn

  • Implement fundamental programming concepts through C programs
  • Understand the importance of creating complex data types and the functions to manipulate them
  • Develop good coding practices and learn to write clean code
  • Validate your programs before developing them further
  • Use the C Standard Library functions and understand why it is advantageous
  • Build and run a multi-file program with Make
  • Get an overview of how C has changed since its introduction and where it is going

Who this book is for

If you're an absolute beginner who has basic familiarity with operating a computer, this book will help you learn the most fundamental concepts and practices that you need to know to become a successful C programmer. If you're an experienced programmer, you'll find the full range of C syntax as well as common C idioms covered in the book useful.

Table of contents

  1. Learn C Programming
Second Edition
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Preface to the Second Edition
    2. Preface to the First Edition
    3. Who this book is for
    4. What this book covers
    5. To get the most out of this book
    6. Download the example code files
    7. Download the color images
    8. Conventions used
    9. Get in touch
    10. Share Your Thoughts
  6. Part 1: C Fundamentals
  7. Chapter 1:Running Hello, World!
    1. Technical requirements
    2. Writing your first C program
      1. Hello, world!
    3. Understanding the program development cycle
      1. Edit
      2. Compile
      3. Run
      4. Verify
      5. Repeat
    4. Creating, typing, and saving your first C program
    5. Compiling your first C program
    6. Running your first C program
    7. Writing comments to clarify the program later
      1. Some guidelines on commenting code
      2. Adding comments to the Hello, world! program
    8. Learning to experiment with code
    9. Summary
  8. Chapter 2: Understanding Program Structure
    1. Technical requirements
    2. Introducing statements and blocks
      1. Experimenting with statements and blocks
      2. Understanding delimiters
      3. Understanding whitespace
      4. Introducing statements
    3. Introducing functions
    4. Understanding function definitions
      1. Exploring function identifiers
      2. Exploring the function block
      3. Exploring function return values
      4. Passing in values with function parameters
    5. Order of execution
    6. Understanding function declarations
    7. Summary
    8. Questions
  9. Chapter 3: Working with Basic Data Types
    1. Technical requirements
    2. Understanding data types
    3. Bytes and chunks of data
    4. Representing whole numbers
      1. Representing positive and negative whole numbers
      2. Specifying different sizes of integers
    5. Representing numbers with fractions
    6. Representing single characters
    7. Representing Boolean true/false
    8. Understanding the sizes of data types
      1. The sizeof() operator
      2. Ranges of values
    9. Summary
    10. Questions
  10. Chapter 4: Using Variables and Assignments
    1. Technical requirements
    2. Understanding data types and values
    3. Introducing variables
      1. Naming variables
      2. Introducing explicit types of variables
      3. Using explicit typing with initialization
    4. Exploring constants
      1. Literal constants
      2. Defined values
      3. Explicitly typed constants
      4. Naming constant variables
    5. Using types and assignments
      1. Using explicit assignment – the simplest statement
      2. Assigning values by passing function parameters
      3. Assignment by the function return value
    6. Summary
    7. Questions
  11. Chapter 5: Exploring Operators and Expressions
    1. Technical requirements
    2. Understanding expressions and operations
    3. Introducing operations on numbers
      1. Considering the special issues resulting from operations on numbers
    4. Exploring type conversion
      1. Understanding implicit type conversion and values
      2. Using explicit type conversion – casting
    5. Introducing operations on characters
      1. Logical operators
      2. Relational operators
      3. Bitwise operators
      4. The conditional operator
      5. The sequence operator
      6. Compound assignment operators
      7. Incremental operators
    6. Order of operations and grouping
    7. Summary
    8. Questions
  12. Chapter 6: Exploring Conditional Program Flow
    1. Technical requirements
    2. Understanding conditional expressions
    3. Introducing the if()… else… complex statement
    4. Using a switch()… complex statement
    5. Introducing multiple if()… statements
    6. Using nested if()… else… statements
      1. The dangling else problem
    7. Summary
    8. Questions
  13. Chapter 7: Exploring Loops and Iterations
    1. Technical requirements
    2. Understanding repetition
    3. Understanding brute-force repetition
    4. Introducing the while()… statement
    5. Introducing the for()… statement
    6. Introducing the do … while() statement
    7. Understanding loop equivalency
    8. Understanding unconditional branching – the dos and (mostly) don'ts of goto
    9. Further controlling loops with break and continue
    10. Understanding infinite loops
    11. Summary
    12. Questions
  14. Chapter 8: Creating and Using Enumerations
    1. Technical requirements
    2. Introducing enumerations
      1. Defining enumerations
      2. Using enumerations
    3. The switch()… statement revisited
    4. Using enumerated item values as literal integer constants
    5. Summary
    6. Questions
  15. Part 2: Complex Data Types
  16. Chapter 9: Creating and Using Structures
    1. Technical requirements
    2. Understanding structures
      1. Declaring structures
      2. Initializing structures and accessing structure elements
    3. Performing operations on structures – functions
    4. Structures of structures
      1. Initializing structures with functions
      2. Printing a structure of structures – reusing functions
    5. The stepping stone to OOP
    6. Summary
    7. Questions
  17. Chapter 10: Creating Custom Data Types with typedef
    1. Technical requirements
    2. Renaming intrinsic types with typedef
      1. Using synonyms
    3. Simplifying the use of enum types with typedef
    4. Simplifying the use of struct types with typedef
      1. Other uses of typedef
    5. Exploring some more useful compiler options
    6. Using a header file for custom types and the typedef specifiers
    7. Summary
    8. Questions
  18. Chapter 11: Working with Arrays
    1. Technical requirements
    2. Declaring and initializing arrays
      1. Initializing arrays
    3. Understanding variable-length arrays
    4. Accessing array elements
      1. Assigning values to array elements
    5. Operating on arrays with loops
    6. Using functions that operate on arrays
    7. Summary
    8. Questions
  19. Chapter 12: Working with Multi-Dimensional Arrays
    1. Technical requirements
    2. Going beyond 1D arrays to multi-dimensional arrays
      1. Revisiting 1D arrays
      2. Moving on to 2D arrays
      3. Moving on to 3D arrays
      4. Considering N-dimensional arrays
    3. Declaring and initializing multi-dimensional arrays of various dimensions
      1. Declaring arrays of two dimensions
      2. Initializing arrays of two dimensions
      3. Declaring arrays of three dimensions
      4. Initializing arrays of three dimensions
      5. Declaring and initializing arrays of N dimensions
    4. Accessing elements of multi-dimensional arrays of various dimensions
    5. Manipulating multi-dimensional arrays using loops within loops
      1. Using nested loops to traverse a 2D array
      2. Using nested loops to traverse a 3D array
    6. Using multi-dimensional arrays in functions
    7. Summary
    8. Questions
  20. Chapter 13: Using Pointers
    1. Technical requirements
    2. Addressing pointers – the boogeyman of C programming
      1. Why use pointers at all?
    3. Introducing pointers
      1. Understanding direct addressing and indirect addressing
      2. Understanding memory and memory addressing
      3. Managing and accessing memory
      4. Exploring some analogies in the real world
    4. Declaring the pointer type, naming pointers, and assigning addresses
      1. Declaring the pointer type
      2. Naming pointers
      3. Assigning pointer values (addresses)
    5. Operations with pointers
      1. Assigning pointer values
      2. Differentiating between the NULL pointer and void*
      3. Accessing pointer targets
      4. Understanding the void* type
      5. Pointer arithmetic
      6. Comparing pointers
    6. Verbalizing pointer operations
    7. Variable function arguments
      1. Passing values by reference
      2. Passing addresses to functions without pointer variables 
      3. Pointers to pointers
    8. Using pointers to structures
      1. Accessing structures and their elements via pointers
      2. Using pointers to structures in functions
    9. Summary
    10. Questions
  21. Chapter 14: Understanding Arrays and Pointers
    1. Technical requirements
    2. Understanding array names and pointers
    3. Understanding array elements and pointers
      1. Accessing array elements via pointers
    4. Operations on arrays using pointers
      1. Using pointer arithmetic
      2. Passing arrays as function pointers revisited
      3. Interchangeability of array names and pointers
    5. Introducing an array of pointers to arrays 
    6. Summary
    7. Questions
  22. Chapter 15: Working with Strings
    1. Technical requirements
    2. Characters – the building blocks of strings
      1. The char type and ASCII
      2. Beyond ASCII – UTF-8 and Unicode
      3. Operations on characters
      4. Getting information about characters
      5. Manipulating characters
    3. Exploring C strings
      1. An array with a terminator
      2. Strengths of C strings
      3. Weaknesses of C strings
    4. Declaring and initializing a string
      1. String declarations
      2. Initializing strings
      3. Passing a string to a function
      4. Empty strings versus null strings
      5. Hello, World! revisited
    5. Creating and using an array of strings
    6. Common operations on strings – the standard library
      1. Common functions
      2. Safer string operations
    7. Summary
    8. Questions
  23. Chapter 16: Creating and Using More Complex Structures
    1. Technical requirements
    2. Introducing the need for complex structures
    3. Revisiting card4.h
    4. Understanding an array of structures
      1. Creating an array of structures
      2. Accessing structure elements within an array
      3. Manipulating an array of structures
    5. Using a structure with other structures
      1. Creating a structure consisting of other structures
      2. Accessing structure elements within the structure
      3. Manipulating a structure consisting of other structures
    6. Using a structure with arrays
      1. Understanding randomness and random number generators
      2. Creating a structure with an array
      3. Accessing array elements within a structure
      4. Manipulating array elements within a structure
    7. Using a structure with an array of structures
      1. Creating a structure with an array of structures
      2. Accessing individual structure elements of the array within a structure
      3. Manipulating a structure with an array of structures
      4. Completing carddeck.c
    8. Summary
    9. Questions
  24. Part 3: Memory Manipulation
  25. Chapter 17: Understanding Memory Allocation and Lifetime
    1. Technical requirements
    2. Defining storage classes
    3. Understanding automatic versus dynamic storage classes
      1. Automatic storage
      2. Dynamic storage
    4. Understanding internal versus external storage classes
      1. Internal or local storage classes
      2. External or global storage classes
      3. The lifetime of automatic storage
    5. Exploring the static storage class
      1. Internal static storage
      2. External static storage
      3. The lifetime of static storage
    6. Summary
    7. Questions
  26. Chapter 18: Using Dynamic Memory Allocation
    1. Technical requirements
    2. Introducing dynamic memory
      1. A brief tour of C's memory layout
    3. Allocating and releasing dynamic memory
      1. Allocating dynamic memory
      2. Releasing dynamic memory
      3. Accessing dynamic memory
      4. The lifetime of dynamic memory
    4. Special considerations for dynamic allocation
      1. Heap memory management
    5. The linked list dynamic data structure
      1. Linked list structures
      2. Declaring operations on a linked list
    6. Pointers to functions
      1. More complex operations on a linked list
      2. A program to test our linked list structure
    7. Other dynamic data structures
    8. Summary
    9. Questions
  27. Part 4: Input and Output
  28. Chapter 19: Exploring Formatted Output
    1. Technical requirements
    2. Revisiting printf()
      1. Understanding the general format specifier form
    3. Using format specifiers for unsigned integers
      1. Using unsigned integers in different bases
      2. Considering negative numbers as unsigned integers
      3. Exploring the powers of 2 and 9 in different bases
      4. Printing pointer values
    4. Using format specifiers for signed integers
      1. Using the signed integer field width, precision, alignment, and zero-filling
      2. Formatting long-long integers
      3. Powers of 2 and 9 with different modifiers
    5. Using format specifiers for floats and doubles
      1. Using the floating-point field width, precision, alignment, and zero-filling
      2. Printing doubles in hexadecimal format
      3. Printing optimal field widths for doubles
    6. Using format specifiers for strings and characters
      1. Using the string field width, precision, alignment, and zero-filling
      2. Exploring the substring output
      3. Using single character formatting
    7. Summary
    8. Questions
  29. Chapter 20: Getting Input from the Command Line
    1. Technical requirements
    2. Revisiting the main() function
      1. The special features of main()
      2. The two forms of main()
    3. Using argc and argv
      1. Simple use of argc and argv
      2. Command-line switches and command-line processors
    4. Summary
    5. Questions
  30. Chapter 21: Exploring Formatted Input
    1. Technical requirements
    2. Introducing streams
      1. Understanding the standard output stream
      2. Understanding the standard input stream
      3. Revisiting the console output with printf() and fprintf()
      4. Exploring the console input with scanf()
    3. Reading formatted input with scanf()
      1. Reading numerical input with scanf()
      2. Reading string and character input with scanf()
      3. Using a scan set to limit possible input characters
      4. Controlling the scanf() input field width 
    4. Using internal data conversion
      1. Using sscanf() and sprintf() to convert values into and from strings
      2. Converting strings into numbers with atoi() and atod()
    5. Exploring unformatted I/O
      1. Getting the string I/O to/from the console
      2. Using the simple I/O of strings with gets() and puts()
      3. Creating a sorted list of names with fgets() and fputs()
    6. Summary
    7. Questions
  31. Chapter 22: Working with Files
    1. Technical requirements
    2. Understanding basic file concepts
      1. Revisiting file streams
      2. Understanding the properties of FILE streams
      3. Opening and closing a file
      4. Understanding file operations for each type of stream
    3. Introducing the filesystem essentials
      1. Getting acquainted with the filesystem
    4. Opening files for reading and writing
      1. Getting filenames from within a program
      2. Getting filenames from the command line 
    5. Summary
    6. Questions
  32. Chapter 23: Using File Input and File Output
    1. Technical requirements
    2. File processing
      1. Processing filenames from the command line
    3. Creating a file of unsorted names
      1. Trimming the input string from fgets()
      2. Reading names and writing names
    4. Reading unsorted names and sorting them 
for output
      1. Using a linked list to sort names
      2. Writing names in sorted order
    5. Summary
    6. Questions
  33. Part 5: Building Blocks for Larger Programs
  34. Chapter 24: Working with Multi-File Programs
    1. Technical requirements
    2. Understanding multi-file programs
    3. Using header files for declarations and source files for definitions
      1. Creating source files
      2. Thinking about multiple source files
      3. Creating header files
    4. Revisiting the preprocessor
      1. Introducing preprocessor directives
      2. Understanding the limits and dangers of the preprocessor
      3. Using the preprocessor effectively
      4. Debugging with the preprocessor
    5. Creating a multi-file program
      1. Extracting card structures and functions
      2. Extracting hand structures and functions
      3. Extracting deck structures and functions
      4. Finishing the dealer.c program
    6. Building a multi-file program
    7. Summary
    8. Questions
  35. Chapter 25: Understanding Scope
    1. Technical requirements
    2. Defining scope – visibility, extent, and linkage
      1. Exploring visibility
      2. Exploring extent
      3. Exploring linkage
      4. Putting visibility, extent, and linkage all together
    3. Exploring variable scope
    4. Understanding the block scope of variables
      1. Understanding function parameter scope
      2. Understanding file scope
      3. Understanding global scope
    5. Understanding scope for functions
      1. Understanding scope and information hiding
      2. Using the static specifier for functions
    6. Summary
    7. Questions
  36. Chapter 26: Building Multi-File Programs with Make
    1. Technical requirements
    2. Preparing dealer.c for make
    3. Introducing the make utility
    4. Using macros
      1. Using special macros
    5. Creating rules – targets, dependencies, and actions
      1. Creating useful rules that have only actions
      2. Pattern rules and %
      3. Built-in functions
      4. Caveman debugging in your makefile
    6. A general makefile for simple projects
    7. Going further with make
    8. Summary
    9. Questions
  37. Chapter 27:Creating Two Card Programs
    1. Technical requirements
    2. Game one – Blackjack
      1. Introducing Blackjack
      2. Determining which structures and functions will be needed
      3. Considering user interaction and the limits of a console interface
      4. Preparing source files for Blackjack
      5. Implementing Blackjack
      6. Considering ways Blackjack could be extended
    3. Game two – One-Handed Solitaire
      1. Introducing One-Handed Solitaire
      2. Determining which structures and functions will be needed
      3. Considering user interactions and the limits of a console interface
      4. Preparing source files for One-Handed Solitaire
      5. Implementing One-Handed Solitaire
      6. Considering ways One-Handed Solitaire could be extended
    4. Summary
  38. Appendix
    1. C definition and keywords
    2. C keywords
    3. Table of operators and their precedence
    4. Summary of useful GCC and Clang compiler options
    5. ASCII character set
    6. The Better String Library (Bstrlib)
    7. A quick introduction to Bstrlib
    8. A few simple examples
    9. Unicode and UTF-8
      1. A brief history
      2. Where we are today
      3. Moving from ASCII to UTF-8
    10. A UTF to Unicode example
    11. The C standard library
    12. Method 1
    13. Method 2
    14. Method 3
  39. Epilogue
    1. Taking the next steps
  40. Assessments
    1. Chapter 2 – Understanding Program Structure
    2. Chapter 3 – Working with Basic Data Types
    3. Chapter 4 – Using Variables and Assignments
    4. Chapter 5 – Exploring Operators 
and Expressions
    5. Chapter 6 – Exploring Conditional 
Program Flow
    6. Chapter 7 – Exploring Loops and Iteration
    7. Chapter 8 – Creating and Using Enumerations
    8. Chapter 9 – Creating and Using Structures
    9. Chapter 10 – Creating Custom Data Types 
with typedef
    10. Chapter 11 – Working with Arrays
    11. Chapter 12 – Working with Multi-Dimensional Arrays
    12. Chapter 13 – Using Pointers
    13. Chapter 14 – Understanding Arrays and Pointers
    14. Chapter 15 – Working with Strings
    15. Chapter 16 – Creating and Using More Complex Structures
    16. Chapter 17 – Understanding Memory Allocation and Lifetime
    17. Chapter 18 – Using Dynamic 
Memory Allocation
    18. Chapter 19 – Exploring Formatted Output
    19. Chapter 20 – Getting Input from the Command Line
    20. Chapter 21 – Exploring Formatted Input
    21. Chapter 22 – Working with Files
    22. Chapter 23 – Using File Input and File Output
    23. Chapter 24 – Working with Multi-File Programs
    24. Chapter 25 – Understanding Scope
    25. Why subscribe?
  41. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Learn C Programming - Second Edition
  • Author(s): Jeff Szuhay
  • Release date: August 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801078450