Learn C Programming: A beginner's guide to learning the most powerful and general-purpose programming language with ease

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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. Running Hello, world
  2. Understanding Program Structure
  3. Working with Basic Data Types
  4. Using Variables and Assignments
  5. Exploring Operators and Expressions
  6. Exploring Conditional Program Flow
  7. Exploring Loops and Iterations
  8. Creating and Using Enumerations
  9. Creating and Using Structures
  10. Creating Custom Data Types with Typedef
  11. Working with Arrays
  12. Working with Multi-dimensional Arrays
  13. Using Pointers
  14. Understanding Arrays and Pointers
  15. Working with Strings
  16. Creating and Using More Complex Structures
  17. Understanding Memory Allocation and Lifetime
  18. Using Dynamic Memory Allocation
  19. Exploring Formatted Output
  20. Getting Input From the Command Line
  21. Exploring Formatted Input
  22. Working with Files
  23. Using File Input and File Output
  24. Working with Multi-File Programs
  25. Understanding Scope
  26. Building Multi-File Programs with Make
  27. Creating Two Card Programs
  28. Appendix

Author(s): Jeff Szuhay
Edition: 2
Publisher: Packt Publishing
Year: 2022

Language: English
Commentary: Publisher PDF
Pages: 742
City: Birmingham, UK
Tags: C Programming; C99; C11; C17

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