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, arrays, 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
- Title Page
- Copyright and Credits
- Dedication
- About Packt
- Contributors
- Preface
- Section 1: C Fundamentals
-
Running Hello, World!
- Technical requirements
- Writing your first C program
- Hello, world!
- Understanding the program development cycle
- Edit
- Compile
- Many C compilers for every OS
- A note about IDEs
- Installing a compiler on Linux, macOS, or Windows
- Run
- Verify
- Repeat
- A note about debugging
- 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
-
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
-
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 decimals
- Representing single characters
- Representing Boolean true/false
- Understanding the sizes of data types
- The sizeof() operator
- Ranges of values
- Summary
-
Using Variables and Assignment
- Technical requirements
- Understanding 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 assignment
- Using explicit assignment, the simplest statement
- Assigning values by passing function parameters
- Assignment by the function return value
- Summary
-
Exploring Operators and Expressions
- Technical requirements
- Expressions and operations
- Introducing operations on numbers
- Considering the special issues resulting from operations on numbers
- Understanding NaN
- Understanding underflow NaN
- Understanding overflow NaN
- Considering precision
- Exploring type conversion
- Understanding implicit type conversion and values
- Using explicit type conversion – casting
- Introducing operations on characters
- Exploring logical and relational operators
- Bitwise operators
- The conditional operator
- The sequence operator
- Compound assignment operators
- Multiple assignments in a single expression
- Incremental operators
- Postfix versus prefix incrementation
- Order of operations and grouping
- Summary
- Exploring Conditional Program Flow
-
Exploring Loops and Iteration
- 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
- Creating and Using Enumerations
- Section 2: Complex Data Types
-
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 object-oriented programming
- Summary
- Creating Custom Data Types with typedef
- Working with Arrays
-
Working with Multi-Dimensional Arrays
- Technical requirements
- Going beyond one-dimensional arrays to multi-dimensional arrays
- Revisiting one-dimensional arrays
- Moving on to two-dimensional arrays
- Moving on to three-dimensional arrays
- Considering N-dimensional arrays
- Declaring and initializing multi-dimensional arrays
- 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
- Manipulating multi-dimensional arrays – loops within loops
- Using nested loops to traverse a two-dimensional array
- Using nested loops to traverse a three-dimensional array
- Using multi-dimensional arrays in functions
- Summary
-
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*
- Understanding the void* type
- Accessing pointer targets
- 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
-
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
- Using the increment operator
- Passing arrays as function pointers revisited
- Interchangeability of array names and pointers
- Introducing an array of pointers to arrays
- Summary
-
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
-
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
- Revisiting the hand structure
- Revisiting hand operations
- 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
- Revisiting the deck structure
- Revisiting deck operations
- A basic card program
- Summary
- Section 3: Memory Manipulation
-
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
-
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
- Memory leaks
- 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
- Section 4: Input and Output
-
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 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 sub-string output
- Using single character formatting
- Summary
- Getting Input from the Command Line
-
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 input and output
- Getting the string input and output to/from the console
- Using the simple input and output of strings with gets() and puts()
- Understanding why using gets() could be dangerous
- Creating a sorted list of names with fgets() and fputs()
- Summary
-
Working with Files
- Technical requirements
- Understanding basic file concepts
- Revisiting file streams
- Understanding the properties of the FILE streams
- Opening and closing a file
- Understanding file operations for each type of stream
- Introducing the filesystem essentials
- Introducing the filesystem
- Understanding a file path
- Understanding a filename
- Opening files for reading and writing
- Getting filenames from within the program
- Getting filenames from the command line
- Summary
-
Using File Input and File Output
- Technical requirements
- File processing
- Creating a template program to process filenames given on 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
- Section 5: Building Blocks for Larger Programs
-
Working with Multi-File Programs
- Technical requirements
- Understanding multi-file programs
- Using header files for declarations and source files for definitions
- Creating source files
- Creating header files
- Revisiting the preprocessor
- Understanding the limits and dangers of the preprocessor
- Knowing some 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
-
Understanding Scope
- Technical requirements
- Defining scope – visibility, extent, and linkage
- Exploring visibility
- Exploring extent
- Exploring linkage
- Understanding compilation units
- 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 function scope
- Understanding scope and information hiding
- Using the static specifier for functions
- Summary
- Epilog
- Taking the next steps
- More advanced C topics
- More advanced programming topics
- Picking a project for yourself
- Resources
-
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
- Other Books You May Enjoy
Product information
- Title: Learn C Programming
- Author(s):
- Release date: June 2020
- Publisher(s): Packt Publishing
- ISBN: 9781789349917
You might also like
book
Learn C Programming - Second Edition
Get started with writing simple programs in C while learning core programming concepts Key Features Learn …
book
Programming in C, Fourth Edition
will teach you how to write programs in the C programming language. Whether you’re a novice …
video
Learn and Master C Programming For Absolute Beginners!
In this course, we'll explore the C programming language from the ground up. We'll begin with …
book
C Programming Language, 2nd Edition
This book is meant to help the reader learn how to program in C. It is …