Home Page Icon
Home Page
Table of Contents for
Cover
Close
Cover
by Ben Klemens
21st Century C, 2nd Edition
Preface
C Is Punk Rock
Q & A (Or, the Parameters of the Book)
Standards: So Many to Choose From
The POSIX Standard
Some Logistics
The Second Edition
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
I. The Environment
1. Set Yourself Up for Easy Compilation
Use a Package Manager
Compiling C with Windows
POSIX for Windows
Compiling C with POSIX
Compiling C Without POSIX
Which Way to the Library?
A Few of My Favorite Flags
Paths
Runtime Linking
Using Makefiles
Setting Variables
The Rules
Using Libraries from Source
Using Libraries from Source (Even if Your Sysadmin Doesn’t Want You To)
Compiling C Programs via Here Document
Include Header Files from the Command Line
The Unified Header
Here Documents
Compiling from stdin
2. Debug, Test, Document
Using a Debugger
A Debugging Detective Story
GDB Variables
Print Your Structures
Using Valgrind to Check for Errors
Unit Testing
Using a Program as a Library
Coverage
Error Checking
What is the User’s Involvement in the Error?
The Context in Which the User is Working
How Should the Error Indication Be Returned?
Interweaving Documentation
Doxygen
Literate Code with CWEB
3. Packaging Your Project
The Shell
Replacing Shell Commands with Their Outputs
Use the Shell’s for Loops to Operate on a Set of Files
Test for Files
fc
Makefiles vs. Shell Scripts
Packaging Your Code with Autotools
An Autotools Demo
Describing the Makefile with Makefile.am
The configure Script
4. Version Control
Changes via diff
Git’s Objects
The Stash
Trees and Their Branches
Merging
The Rebase
Remote Repositories
5. Playing Nice with Others
Dynamic Loading
The Limits of Dynamic Loading
The Process
Writing to Be Read by Nonnatives
The Wrapper Function
Smuggling Data Structures Across the Border
Linking
Python Host
Compiling and Linking
The Conditional Subdirectory for Automake
Distutils Backed with Autotools
II. The Language
6. Your Pal the Pointer
Automatic, Static, and Manual Memory
Persistent State Variables
Pointers Without malloc
Structures Get Copied, Arrays Get Aliased
malloc and Memory-Twiddling
The Fault Is in Our Stars
All the Pointer Arithmetic You Need to Know
Typedef as a teaching tool
7. Inessential C Syntax that Textbooks Spend a Lot of Time Covering
Don’t Bother Explicitly Returning from main
Let Declarations Flow
Set Array Size at Runtime
Cast Less
Enums and Strings
Labels, gotos, switches, and breaks
goto Considered
switch
Deprecate Float
Comparing Unsigned Integers
Safely Parse Strings to Numbers
8. Important C Syntax that Textbooks Often Do Not Cover
Cultivate Robust and Flourishing Macros
The Preprocessor
Test Macros
Header Guards
Linkage with static and extern
Externally Linked Variables in Header Files
The const Keyword
Noun-Adjective Form
Tension
Depth
The char const ** Issue
9. Easier Text Handling
Making String Handling Less Painful with asprintf
Security
Constant Strings
Extending Strings with asprintf
A Pæan to strtok
Unicode
The Encoding for C Code
Unicode Libraries
The Sample Code
10. Better Structures
Compound Literals
Initialization via Compound Literals
Variadic Macros
Safely Terminated Lists
Multiple Lists
Foreach
Vectorize a Function
Designated Initializers
Initialize Arrays and Structs with Zeros
Typedefs Save the Day
A Style Note
Return Multiple Items from a Function
Reporting Errors
Flexible Function Inputs
Declare Your Function as printf-Style
Optional and Named Arguments
Polishing a Dull Function
The Void Pointer and the Structures It Points To
Functions with Generic Inputs
Generic Structures
11. Object-Oriented Programming in C
Extending Structures and Dictionaries
Implementing a Dictionary
C, with fewer seams
Functions in Your Structs
Vtables
Scope
Private Struct Elements
Overload
_Generic
Count References
Example: A Substring Object
Example: An Agent-Based Model of Group Formation
Conclusion
12. Parallel Threads
The Environment
The Ingredients
OpenMP
Compiling OpenMP, pthreads, and C atoms
Interference
Map-reduce
Multiple Tasks
Thread Local
Localizing Nonstatic Variables
Shared Resources
Atoms
Pthreads
C atoms
Atomic structs
13. Libraries
GLib
POSIX
Parsing Regular Expressions
Using mmap for Gigantic Data Sets
The GNU Scientific Library
SQLite
The Queries
libxml and cURL
Epilogue
A. C 101
The Structure
C requires a compilation step, which consists of running a single command
There’s a standard library, and it’s part of your operating system
There’s a preprocessor
There are two types of comment
There is no print keyword
Variable Declarations
Variables have to be declared
Even functions have to be declared or defined
Basic types can be aggregated into arrays and structs
New structure types can be defined
You can find out how much space a type takes
There is no special string type
Expressions
The scoping rules for C are very simple
The main function is special
Most of what a C program actually does is evaluate expressions
Functions are evaluated using copies of the inputs
Expressions are delimited by semicolons
There are many shortcuts for incrementing or scaling a variable
C has an expansive definition of truth
Dividing two integers always produces an integer
C has a trinary conditional operator
Branching and looping expressions are not very different from any other language
The for loop is just a compact version of the while loop
Pointers
You can directly request a block of memory
Arrays are just blocks of memory; any block of memory can be used like an array
A pointer to a scalar is really just a one-item array
There is a special notation for elements of pointed-to structs
Pointers let you modify function inputs
Everything is somewhere, so everything can be pointed to
Glossary
References
Index
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
21st Century C
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset