Martin Kalin

Modern C Up and Running

A Programmer's Guide to Finding Fluency and Bypassing the Quirks

Martin Kalin
Chicago, IL, USA
ISBN 978-1-4842-8675-3e-ISBN 978-1-4842-8676-0
© Martin Kalin 2022
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

This Apress imprint is published by the registered company APress Media, LLC, part of Springer Nature.

The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.

To Janet, yet again.

Preface

1. Why C?

C is a small but extensible language, with software libraries (standard and third party) extending the core language. Among high-level languages, C still sets the mark for performance; hence, C is well suited for applications, especially ones such as database systems and web servers that must perform at a high level. The syntax for C is straightforward, but with an oddity here and there. Anyone who programs in a contemporary high-level language already knows much of C syntax, as other languages have borrowed widely from C.

C is also the dominant systems language: modern operating systems are written mostly in C, with assembly language accounting for the rest. Other programming languages routinely and transparently use standard library routines written in C. For example, when an application written in any other high-level language prints the Hello, world! greeting, it is a C library function that ultimately writes the message to the screen. The standard system libraries for input/output, networking, string processing, mathematics, security, cryptography, data encoding, and so on are likewise written mainly in C. To write a program in C is to write in the system’s native language.

Who’s the Intended Audience?
This book is for programmers and assumes experience in a general-purpose language—but none in C. You should be able to work from the command line. Linux and macOS come with a C compiler, typically GNU C ( https://gcc.gnu.org ) and Clang ( https://clang.llvm.org ), respectively. At the command-line prompt (% is used here), the command
% gcc -v

should provide details. For Windows, Cygwin ( https://cygwin.com/install.html ) is recommended.

C has been a modern language from the start. The familiar function, which can take arguments and return a value, is the primary code module in C. C exhibits a separation of concerns by distinguishing between interfaces, which describe how functions are called, and implementations, which provide the operational details. As noted, C is naturally and easily extended through software libraries, whether standard or third party. As these libraries become better and richer, so does C. C programmers can create arbitrarily rich data types and data structures and package their own code modules as reusable libraries. C supports higher-order functions (functions that can take functions as arguments) without any special, fussy syntax. This book covers C’s modern features, but always with an eye on C’s close-to-the-metal features.

To understand C is to understand the underlying architecture of a modern computing machine, from an embedded device through a handheld up to a node in a server cluster. C sits atop assembly language, which is symbolic (human-understandable) machine language. Every assembly language is specific to a computer architecture. The assembly language for an Intel device differs from that of an ARM device. Even within an architectural family such as Intel, changes in the architecture are reflected in assembly language. As symbolic machine language, assembly language is approachable, although reading and writing assembly code can be daunting. Assembly language is of interest even to programmers in other languages because it reveals so much about the underlying system. C does not reveal quite as much, but far more than any other high-level language; C also reveals what is common across architectures. One sign of just how close C is to assembly language shows up in compilation: a C compiler can handle any mix of C and assembly source code, and C source is translated first into assembly code. From time to time, it will be useful to compare C source with the assembly source into which the C source translates.

C source code ports well: a C program that compiles on one platform should compile on another, unless platform-specific libraries and data structure sizes come into play. Perfect portability remains an ideal, even for C. C plays still another role—as the lingua franca among programming languages: any language that can talk to C can talk to any other language that does so. Most other languages support C calls in one form or another; a later code example shows how straightforwardly Python can consume library functions written in C.

2. From the Basics Through Advanced Features

This book is code centric, with full program examples and shorter code segments in the forefront throughout. The book begins, of course, with C basics: program structure, built-in data types and control structures, operators, pointers, aggregates such as arrays and structures, input and output, and so on. Here is an overview of some advanced topics:
  • Memory safety and efficiency: Best practices for using the stack, the heap, and static area of memory; techniques and tools for avoiding memory leakage

  • Higher-order functions: Simplifying code by passing functions as arguments to other functions

  • Generic functions: How to use the pointer-to-void (void*) data type in creating and calling generic functions

  • Functions with a variable number of arguments: How to write your own

  • Defining new data types: A convenient way to name programmer-defined, arbitrarily rich data types

  • Clarifying C code through assembly-language code: Getting closer to the metal

  • Embedding assembly code: Checking for overflow with embedded assembly

  • Floating-point issues: Code examples and the IEEE 754 specification in detail

  • Low-level and high-level input/output: Flexibility and performance trade-offs in input/output operations

  • Networking and wire-level security: Full code examples, including digital certificates and secure sockets

  • Nonblocking input/output: Local machine and networking examples of this acceleration technique

  • Concurrency and parallelism: Multiprocessing, interprocess communication, multithreading, deadlock, and instruction-level SIMD parallelism

  • Interprocess communication: Pipes (named and unnamed), message queues, sockets, file sharing and locking, shared memory with a semaphore, and signals

  • Data validation: Regular expressions in detail

  • Internationalization: Standard libraries for locale management

  • Assertions: Expressing and enforcing pre-, post-, and invariant conditions in programs

  • WebAssembly: Compiling C code into WebAssembly for high-performance web modules

  • Software libraries: How to build and deploy both static and dynamic software libraries for C and non-C clients

The code examples in the book are available at https://github.com/mkalin/cbook.git , and comments are welcome at [email protected].

Acknowledgments

My hearty thanks go to the Apress people who made this book possible. I would like to thank, in particular, the following: Steve Anglin, Associate Editorial Director of Acquisitions; Jill Balzano, Coordinating Editor; and James Markham, Development Editor. Thanks as well to the technical reviewers who made the book better than it otherwise would have been.

Table of Contents
Index 345
About the Author
Martin Kalin

has a Ph.D. from Northwestern University and is a professor in the College of Computing and Digital Media at DePaul University. He has cowritten a series of books on C and C++ and written a book on Java web services. He enjoys commercial programming and has codeveloped, in C, large distributed systems in process scheduling and product configuration. He can be reached at http://condor.depaul.edu/mkalin .

 
About the Technical Reviewer
Germán González-Morris

is a polyglot Software Architect/Engineer with 20+ years in the field, with knowledge in Java, Spring, C, Julia, Python, Haskell, and JavaScript, among others. He works for cloud, web distributed applications. Germán loves math puzzles (including reading Knuth), swimming, and table tennis. Also, he has reviewed several books including an application container book (Weblogic) and some books on languages (Haskell, TypeScript, WebAssembly, Math for coders, regexp, Julia, Algorithms). For more details, visit his blog ( https://devwebcl.blogspot.com/ ) or Twitter account: @devwebcl.

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.239.148