0%

Book Description

Get started with writing simple programs in C while learning the skills that will help you work with practically any programming language

Key Features

  • Learn essential C concepts such as variables, data structures, functions, loops, and pointers
  • Get to grips with the core programming aspects that form the base of many modern programming languages
  • Explore the expressiveness and versatility of the C language with the help of sample programs

Book Description

C is a powerful general-purpose programming language that is excellent for beginners to learn. This book will introduce you to computer programming and software development using C. If you're an experienced developer, this book will help you to become familiar with the C programming language.

This C programming book takes you through basic programming concepts and shows you how to implement them in C. Throughout the book, you'll create and run programs that make use of one or more C concepts, such as program structure with functions, data types, and conditional statements. You'll also see how to use looping and iteration, arrays, pointers, and strings. As you make progress, you'll cover code documentation, testing and validation methods, basic input/output, and how to write complete programs in C.

By the end of the book, you'll have developed basic programming skills in C, that you can apply to other programming languages and will develop a solid foundation for you to advance as a programmer.

What you will learn

  • Understand fundamental programming concepts and implement them in C
  • Write working programs with an emphasis on code indentation and readability
  • Break existing programs intentionally and learn how to debug code
  • Adopt good coding practices and develop a clean coding style
  • Explore general programming concepts that are applicable to more advanced projects
  • Discover how you can use building blocks to make more complex and interesting programs
  • Use C Standard Library functions and understand why doing this is desirable

Who this book is for

This book is written for two very diverse audiences.

If you're an absolute beginner who only has basic familiarity with operating a computer, this book will help you learn the most fundamental concepts and practices 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. You can skim through the explanations and focus primarily on the source code provided.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Learn C Programming
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  7. Section 1: C Fundamentals
  8. Running Hello, World!
    1. Technical requirements
    2. Writing your first C program
    3. Hello, world!
    4. Understanding the program development cycle
    5. Edit
    6. Compile
    7. Many C compilers for every OS
    8. A note about IDEs
    9. Installing a compiler on Linux, macOS, or Windows
    10. Run
    11. Verify
    12. Repeat
    13. A note about debugging
    14. Creating, typing, and saving your first C program
    15. Compiling your first C program
    16. Running your first C program
    17. Writing comments to clarify the program later
    18. Some guidelines on commenting code
    19. Adding comments to the Hello, world! program
    20. Learning to experiment with code
    21. Summary
  9. Understanding Program Structure
    1. Technical requirements
    2. Introducing statements and blocks
    3. Experimenting with statements and blocks
    4. Understanding delimiters
    5. Understanding whitespace
    6. Introducing statements
    7. Introducing functions
    8. Understanding function definitions
    9. Exploring function identifiers
    10. Exploring the function block
    11. Exploring function return values
    12. Passing in values with function parameters
    13. Order of execution
    14. Understanding function declarations
    15. Summary
  10. Working with Basic Data Types
    1. Technical requirements
    2. Understanding data types
    3. Bytes and chunks of data
    4. Representing whole numbers
    5. Representing positive and negative whole numbers
    6. Specifying different sizes of integers
    7. Representing numbers with decimals
    8. Representing single characters
    9. Representing Boolean true/false
    10. Understanding the sizes of data types
    11. The sizeof() operator
    12. Ranges of values
    13. Summary
  11. Using Variables and Assignment
    1. Technical requirements
    2. Understanding types and values
    3. Introducing variables
    4. Naming variables
    5. Introducing explicit types of variables
    6. Using explicit typing with initialization
    7. Exploring constants
    8. Literal constants
    9. Defined values
    10. Explicitly typed constants
    11. Naming constant variables
    12. Using types and assignment
    13. Using explicit assignment, the simplest statement
    14. Assigning values by passing function parameters
    15. Assignment by the function return value
    16. Summary
  12. Exploring Operators and Expressions
    1. Technical requirements
    2. Expressions and operations
    3. Introducing operations on numbers
    4. Considering the special issues resulting from operations on numbers
    5. Understanding NaN
    6. Understanding underflow NaN
    7. Understanding overflow NaN
    8. Considering precision
    9. Exploring type conversion
    10. Understanding implicit type conversion and values
    11. Using explicit type conversion – casting
    12. Introducing operations on characters
    13. Exploring logical and relational operators
    14. Bitwise operators
    15. The conditional operator
    16. The sequence operator
    17. Compound assignment operators
    18. Multiple assignments in a single expression
    19. Incremental operators
    20. Postfix versus prefix incrementation
    21. Order of operations and grouping
    22. Summary
  13. 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
    7. The dangling else… problem
    8. Summary
  14. Exploring Loops and Iteration
    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
  15. Creating and Using Enumerations
    1. Technical requirements
    2. Introducing enumerations
    3. Defining enumerations
    4. Using enumerations
    5. The switch()… statement revisited
    6. Summary
  16. Section 2: Complex Data Types
  17. Creating and Using Structures
    1. Technical requirements
    2. Understanding structures
    3. Declaring structures
    4. Initializing structures and accessing structure elements
    5. Performing operations on structures – functions
    6. Structures of structures
    7. Initializing structures with functions
    8. Printing a structure of structures – reusing functions
    9. The stepping stone to object-oriented programming
    10. Summary
  18. Creating Custom Data Types with typedef
    1. Technical requirements
    2. Renaming intrinsic types with typedef
    3. Using synonyms
    4. Simplifying the use of enum types with typedef
    5. Simplifying the use of struct types with typedef
    6. Other uses of typedef
    7. Some more useful compiler options
    8. Using a header file for custom types and the typedef specifiers
    9. Summary
  19. Working with Arrays
    1. Technical requirements
    2. Declaring and initializing arrays
    3. Initializing arrays
    4. Accessing array elements
    5. Assigning values to array elements
    6. Operating on arrays with loops
    7. Using functions that operate on arrays
    8. Summary
  20. Working with Multi-Dimensional Arrays
    1. Technical requirements
    2. Going beyond one-dimensional arrays to multi-dimensional arrays
    3. Revisiting one-dimensional arrays
    4. Moving on to two-dimensional arrays
    5. Moving on to three-dimensional arrays
    6. Considering N-dimensional arrays
    7. Declaring and initializing multi-dimensional arrays
    8. Declaring arrays of two dimensions
    9. Initializing arrays of two dimensions
    10. Declaring arrays of three dimensions
    11. Initializing arrays of three dimensions
    12. Declaring and initializing arrays of N dimensions
    13. Accessing elements of multi-dimensional arrays
    14. Manipulating multi-dimensional arrays – loops within loops
    15. Using nested loops to traverse a two-dimensional array
    16. Using nested loops to traverse a three-dimensional array
    17. Using multi-dimensional arrays in functions
    18. Summary
  21. Using Pointers
    1. Technical requirements
    2. Addressing pointers – the boogeyman of C programming
    3. Why use pointers at all?
    4. Introducing pointers
    5. Understanding direct addressing and indirect addressing
    6. Understanding memory and memory addressing
    7. Managing and accessing memory
    8. Exploring some analogies in the real world
    9. Declaring the pointer type, naming pointers, and assigning addresses
    10. Declaring the pointer type
    11. Naming pointers
    12. Assigning pointer values (addresses)
    13. Operations with pointers
    14. Assigning pointer values
    15. Differentiating between the NULL pointer and void*
    16. Understanding the void* type
    17. Accessing pointer targets
    18. Pointer arithmetic
    19. Comparing pointers
    20. Verbalizing pointer operations
    21. Variable function arguments
    22. Passing values by reference
    23. Passing addresses to functions without pointer variables 
    24. Pointers to pointers
    25. Using pointers to structures
    26. Accessing structures and their elements via pointers
    27. Using pointers to structures in functions
    28. Summary
  22. Understanding Arrays and Pointers
    1. Technical requirements
    2. Understanding array names and pointers
    3. Understanding array elements and pointers
    4. Accessing array elements via pointers
    5. Operations on arrays using pointers
    6. Using pointer arithmetic
    7. Using the increment operator
    8. Passing arrays as function pointers revisited
    9. Interchangeability of array names and pointers
    10. Introducing an array of pointers to arrays 
    11. Summary
  23. Working with Strings
    1. Technical requirements
    2. Characters – the building blocks of strings
    3. The char type and ASCII
    4. Beyond ASCII – UTF-8 and Unicode
    5. Operations on characters
    6. Getting information about characters
    7. Manipulating characters
    8. Exploring C strings
    9. An array with a terminator
    10. Strengths of C strings
    11. Weaknesses of C strings
    12. Declaring and initializing a string
    13. String declarations
    14. Initializing strings
    15. Passing a string to a function
    16. Empty strings versus null strings
    17. Hello, World! revisited
    18. Creating and using an array of strings
    19. Common operations on strings – the standard library
    20. Common functions
    21. Safer string operations
    22. Summary
  24. 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
    5. Creating an array of structures
    6. Accessing structure elements within an array
    7. Manipulating an array of structures
    8. Using a structure with other structures
    9. Creating a structure consisting of other structures
    10. Accessing structure elements within the structure
    11. Manipulating a structure consisting of other structures
    12. Using a structure with arrays
    13. Understanding randomness and random number generators
    14. Creating a structure with an array
    15. Accessing array elements within a structure
    16. Manipulating array elements within a structure
    17. Revisiting the hand structure
    18. Revisiting hand operations
    19. Using a structure with an array of structures
    20. Creating a structure with an array of structures
    21. Accessing individual structure elements of the array within a structure
    22. Manipulating a structure with an array of structures
    23. Completing carddeck.c
    24. Revisiting the deck structure
    25. Revisiting deck operations
    26. A basic card program
    27. Summary
  25. Section 3: Memory Manipulation
  26. Understanding Memory Allocation and Lifetime
    1. Technical requirements
    2. Defining storage classes
    3. Understanding automatic versus dynamic storage classes
    4. Automatic storage
    5. Dynamic storage
    6. Understanding internal versus external storage classes
    7. Internal or local storage classes
    8. External or global storage classes
    9. The lifetime of automatic storage
    10. Exploring the static storage class
    11. Internal static storage
    12. External static storage
    13. The lifetime of static storage
    14. Summary
  27. Using Dynamic Memory Allocation
    1. Technical requirements
    2. Introducing dynamic memory
    3. A brief tour of C's memory layout
    4. Allocating and releasing dynamic memory
    5. Allocating dynamic memory
    6. Releasing dynamic memory
    7. Accessing dynamic memory
    8. The lifetime of dynamic memory
    9. Special considerations for dynamic allocation
    10. Heap memory management
    11. Memory leaks
    12. The linked list dynamic data structure
    13. Linked list structures
    14. Declaring operations on a linked list
    15. Pointers to functions
    16. More complex operations on a linked list
    17. A program to test our linked list structure
    18. Other dynamic data structures
    19. Summary
  28. Section 4: Input and Output
  29. Exploring Formatted Output
    1. Technical requirements
    2. Revisiting printf()
    3. Understanding the general format specifier form
    4. Using format specifiers for unsigned integers
    5. Using unsigned integers in different bases
    6. Considering negative numbers as unsigned integers
    7. Exploring powers of 2 and 9 in different bases
    8. Printing pointer values
    9. Using format specifiers for signed integers
    10. Using the signed integer field width, precision, alignment, and zero-filling
    11. Formatting long-long integers
    12. Powers of 2 and 9 with different modifiers
    13. Using format specifiers for floats and doubles
    14. Using the floating-point field width, precision, alignment, and zero-filling
    15. Printing doubles in hexadecimal format
    16. Printing optimal field widths for doubles
    17. Using format specifiers for strings and characters
    18. Using the string field width, precision, alignment, and zero-filling
    19. Exploring the sub-string output
    20. Using single character formatting
    21. Summary
  30. Getting Input from the Command Line
    1. Technical requirements
    2. Revisiting the main() function
    3. The special features of main()
    4. The two forms of main()
    5. Using argc and argv
    6. A simple use of argc and argv
    7. Command-line switches and command-line processors
    8. Summary
  31. Exploring Formatted Input
    1. Technical requirements
    2. Introducing streams
    3. Understanding the standard output stream
    4. Understanding the standard input stream
    5. Revisiting the console output with printf() and fprintf()
    6. Exploring the console input with scanf()
    7. Reading formatted input with scanf()
    8. Reading numerical input with scanf()
    9. Reading string and character input with scanf()
    10. Using a scan set to limit possible input characters
    11. Controlling the scanf() input field width 
    12. Using internal data conversion
    13. Using sscanf() and sprintf() to convert values into and from strings
    14. Converting strings into numbers with atoi() and atod()
    15. Exploring unformatted input and output
    16. Getting the string input and output to/from the console
    17. Using the simple input and output of strings with gets() and puts()
    18. Understanding why using gets() could be dangerous
    19. Creating a sorted list of names with fgets() and fputs()
    20. Summary
  32. Working with Files
    1. Technical requirements
    2. Understanding basic file concepts
    3. Revisiting file streams
    4. Understanding the properties of the FILE streams
    5. Opening and closing a file
    6. Understanding file operations for each type of stream
    7. Introducing the filesystem essentials
    8. Introducing the filesystem
    9. Understanding a file path
    10. Understanding a filename
    11. Opening files for reading and writing
    12. Getting filenames from within the program
    13. Getting filenames from the command line 
    14. Summary
  33. Using File Input and File Output
    1. Technical requirements
    2. File processing
    3. Creating a template program to process filenames given on the command line
    4. Creating a file of unsorted names
    5. Trimming the input string from fgets()
    6. Reading names and writing names
    7. Reading unsorted names and sorting them for output
    8. Using a linked list to sort names
    9. Writing names in sorted order
    10. Summary
  34. Section 5: Building Blocks for Larger Programs
  35. Working with Multi-File Programs
    1. Technical requirements
    2. Understanding multi-file programs
    3. Using header files for declarations and source files for definitions
    4. Creating source files
    5. Creating header files
    6. Revisiting the preprocessor
    7. Understanding the limits and dangers of the preprocessor
    8. Knowing some dangers of the preprocessor
    9. Using the preprocessor effectively
    10. Debugging with the preprocessor
    11. Creating a multi-file program
    12. Extracting Card structures and functions
    13. Extracting Hand structures and functions
    14. Extracting Deck structures and functions
    15. Finishing the dealer.c program
    16. Building a multi-file program
    17. Summary
  36. Understanding Scope
    1. Technical requirements
    2. Defining scope – visibility, extent, and linkage
    3. Exploring visibility
    4. Exploring extent
    5. Exploring linkage
    6. Understanding compilation units
    7. Putting visibility, extent, and linkage all together
    8. Exploring variable scope
    9. Understanding the block scope of variables
    10. Understanding function parameter scope
    11. Understanding file scope
    12. Understanding global scope
    13. Understanding function scope
    14. Understanding scope and information hiding
    15. Using the static specifier for functions
    16. Summary
    17. Epilog
    18. Taking the next steps
    19. More advanced C topics
    20. More advanced programming topics
    21. Picking a project for yourself
    22. Resources
  37. 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
    10. A brief history
    11. Where we are today
    12. Moving from ASCII to UTF-8
    13. A UTF-to-Unicode example
    14. The C standard library
    15. Method 1
    16. Method 2
    17. Method 3
  38. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think
3.133.79.70