Throw out your old ideas of C, and relearn a programming language that’s substantially outgrown its origins. With 21st Century C, you’ll discover up-to-date techniques that are absent from every other C text available. C isn’t just the foundation of modern programming languages, it is a modern language, ideal for writing efficient, state-of-the-art applications. Learn to dump old habits that made sense on mainframes, and pick up the tools you need to use this evolved and aggressively simple language. No matter what programming language you currently champion, you’ll agree that C rocks.
Set up a C programming environment with shell facilities, makefiles, text editors, debuggers, and memory checkers
Use Autotools, C’s de facto cross-platform package manager
Learn which older C concepts should be downplayed or deprecated
Explore problematic C concepts that are too useful to throw out
Solve C’s string-building problems with C-standard and POSIX-standard functions
Use modern syntactic features for functions that take structured inputs
Build high-level object-based libraries and programs
Apply existing C libraries for doing advanced math, talking to Internet servers, and running databases
Author(s): Ben Klemens
Publisher: O'Reilly Media
Year: 2012
Language: English
Pages: 296
Table of Contents
Preface
C Is Punk Rock
Q & A (Or, the Parameters of the Book)
Standards: So Many to Choose From
The POSIX Standard
Some Logistics
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Part I. The Environment
Chapter 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
Chapter 2. Debug, Test, Document
Using a Debugger
GDB Variables
Print Your Structures
Using Valgrind to Check for Errors
Unit Testing
Using a Program as a Library
Coverage
Interweaving Documentation
Doxygen
The narrative
Literate Code with CWEB
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?
Chapter 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
Form variables
Content variables
Adding testing
Adding makefile bits
The configure Script
More Bits of Shell
Chapter 4. Version Control
Changes via diff
Git’s Objects
The Stash
Trees and Their Branches
Merging
The Rebase
Remote Repositories
Chapter 5. Playing Nice with Others
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
Part II. The Language
Chapter 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
Chapter 7. C Syntax You Can Ignore
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
Chapter 8. Obstacles and Opportunity
Cultivate Robust and Flourishing Macros
Preprocessor Tricks
Linkage with static and extern
Declare Externally Linked Elements Only in Header Files
The const Keyword
Noun-Adjective Form
Tension
Depth
The char const ** Issue
Chapter 9. Text
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
Chapter 10. Better Structures
Compound Literals
Initialization via Compound Literals
Variadic Macros
Safely Terminated 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
Chapter 11. Object-Oriented Programming in C
What You Don’t Get (and Why You Won’t Miss It)
Scope
Private struct elements
Overloaded with Operator Overloading
_Generic
Extending Structures and Dictionaries
Extending a Structure
C, with fewer seams
Implementing a Dictionary
Base Your Code on Pointers to Objects
Functions in Your Structs
Count References
Example: A Substring Object
An Agent-Based Model of Group Formation
Chapter 12. Libraries
GLib
POSIX
Using mmap for Gigantic Data Sets
Easy Threading with Pthreads
The pthreads checklist
Protect threaded resources with mutexes
The example
_Thread_local and static variables
The GNU Scientific Library
SQLite
The Queries
libxml and cURL
Epilogue
Glossary
Bibliography
Index