Excerpt. Reprinted by permission. All rights reserved.
INTRODUCTION
C was developed as a system programming language in the 1970s, and even after all this time, it remains incredibly popular. System languages are designed for performance and ease of access to the underlying hardware while providing high-level programming features. While other languages may offer newer language features, their compilers and libraries are typically written in C. Carl Sagan once said, If you wish to make an apple pie from scratch, you must first invent the universe.
The inventors of C did not invent the universe; they designed C to work with a variety of computing hardware and architectures that, in turn, were constrained by physics and mathematics. C is layered directly on top of computing hardware, making it more sensitive to evolving hardware features, such as vectorized instructions, than higher-level languages that typically rely on C for their efficiency.
According to the TIOBE index, C has been either the most popular programming language or second most popular since 2001.1 C is TIOBEs programming language of the year for 2019. The popularity of the C programming language can most likely be attributed to several tenets of the language referred to as the spirit of C:
Trust the programmer. Generally speaking, the C language assumes you know what youre doing and lets you. This isnt always a good thing (for example, if you dont know what youre doing).
Dont prevent the programmer from doing what needs to be done. Because C is a system programming language, it has to be able to handle a variety of low-level tasks.
Keep the language small and simple. The language is designed to be fairly close to the hardware and to have a small footprint.
Provide only one way to do an operation. Also known as conservation of mechanism, the C language tries to limit the introduction of duplicate mechanisms.
Make it fast, even if it isnt guaranteed to be portable. Allowing you to write optimally efficient code is the top priority. The responsibility of ensuring that code is portable, safe, and secure is delegated to you, the programmer.
A Brief History of C
The C programming language was developed in 1972 by Dennis Ritchie and Ken Thompson at Bell Telephone Laboratories. Brian Kernighan coauthored The C Programming Language (K&R 1988) with Dennis Ritchie. In 1983, the American National Standards Institute (ANSI) formed the X3J11 committee to establish a standard C specification, and in 1989, the C Standard was ratified as ANSI X3.159-1989, Programming Language C. This 1989 version of the language is referred to as ANSI C or C89.
In 1990, the ANSI C Standard was adopted (unchanged) by a joint technical committee of the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) and published as the first edition of the C Standard, C90 (ISO/IEC 9899:1990). The second edition of the C Standard, C99, was published in 1999 (ISO/IEC 9899:1999), and a third edition, C11, in 2011 (ISO/IEC 9899:2011). The latest version of the C Standard (as of this writing) is the fourth version, published in 2018 as C17 (ISO/IEC 9899:2018). A new major revision referred to as C2x is under development by ISO/IEC. According to 2018 polling data from JetBrains, 52 percent of C programmers use C99, 36 percent use C11, and 23 percent use an embedded version of C.2
The C Standard
The C Standard (ISO/IEC 9899:2018) defines the language and is the final authority on language behavior. While the standard can be obscure to impenetrable, you need to understand it if you intend to write code thats portable, safe, and secure. The C Standard provides a substantial degree of latitude to implementations to allow them to be optimally efficient on various hardware platforms. Implementations is the term used by the C Standard to refer to compilers and is defined as follows:
A particular set of software, running in a particular translation environment under particular control options, that performs translation of programs for, and supports execution of functions in, a particular execution environment.
This definition indicates that each compiler with a particular set of command line flags, along with the C Standard Library, is considered a separate implementation, and different implementations can have significantly different implementation-defined behavior. This is noticeable in GNU Compiler Collection (GCC), which uses the -std= flag to determine the language standard. Possible values for this option include c89, c90, c99, c11, c17, c18, and c2x. The default depends on the version of the compiler. If no C language dialect options are given, the default for GCC 10 is -std=gnu17, which provides extensions to the C language. For portability, specify the standard youre using. For access to new language features, specify a recent standard. A good choice (in 2019) with GCC 8 and later is -std=c17.
Because implementations have such a range of behaviors, and because some of these behaviors are undefined, you cant understand the C language by just writing simple test programs to examine the behavior. The behavior of the code may vary when compiled by a different implementation on different platforms or even the same implementation using a different set of flags or a different C Standard Library implementation. Code behavior can even vary between versions of a compiler. The C Standard is the only document that specifies which behaviors are guaranteed for all implementations, and where you need to plan for variability. This is mostly a concern when developing portable code, but can also affect the security and safety of your code.
The CERT C Coding Standard
The CERT C Coding Standard, Second Edition: 98 Rules for Developing Safe, Reliable, and Secure Systems (Seacord 2014) is a reference book I wrote while managing the secure coding team at the Software Engineering Institute at Carnegie Mellon University. The CERT C Coding Standard contains examples of common C programming mistakes and how to correct them. Throughout this book, we reference some of these rules as a source for detailed information on specific C language programming topics.
Who This Book Is For
This book is an introduction to the C language. It is written to be as accessible as possible to anyone who wants to learn C programming, without dumbing it down. In other words, we didnt overly simplify C programming in the way many other introductory books and courses might. These overly simplified references will teach you how to get code to compile and run, but the code might still be wrong. Developers who learn how to program C from such sources will typically develop substandard, flawed, insecure code that will eventually need to be rewritten (often sooner than later). Hopefully, these developers will eventually benefit from senior developers in their organizations who will help them unlearn these harmful misconceptions about programming in C, and help them start developing professional quality C code. On the other hand, this book will quickly teach you how to develop correct, portable, professional-quality code, build a foundation for developing security-critical and safety-critical systems, and perhaps teach you a thing or two that even the senior developers at your organization dont know.
Effective C: An Introduction to Professional C Programming is a concise introduction to essential C language programming that will soon have you writing programs, solving problems, and building working systems. The code examples are idiomatic and straightforward. In this book, youll learn about essential programming concepts in C and practice writing high-quality code with exercises for each topic. Youll also learn about good software engineering practices for developing correct, secure C code.
Whats in This Book
This book starts with an introductory chapter that covers just enough material to get you programming right from the start. After this, we circle back and examine the basic building blocks of the language. The book culminates with two chapters that will show you how to compose real-world systems from these basic building blocks and how to debug, test, and analyze the code youve written. The chapters are as follows:
Chapter 1: Getting Started with C Youll write a simple C program to become familiar with using the main function. Youll also look at a few options for editors and compilers.
Chapter 2: Objects, Functions, and Types This chapter explores basics like declaring variables and functions. Youll also look into the principles of using basic types.
Chapter 3: Arithmetic Types Youll learn about the two kinds of arithmetic data types: integers and floating-point types.
Chapter 4: Expressions and Operators Youll learn about operators and how to write simple expressions to perform operations on various object types.
Chapter 5: Control Flow Youll learn how to control the order in which individual statements are evaluated. Well start by going over expression statements and compound statements that define the work to be performed. Well then cover three kinds of statements that determine which code blocks are executed, and in what order: selection, iteration, and jump statements.
Chapter 6: Dynamically Allocated Memory Youll learn about dynamically allocated memory, which is allocated from the heap at runtime. Dynamically allocated memory is useful when the exact storage requirements for a program are unknown before runtime.
Chapter 7: Character and Strings Youll learn about the various character sets, including ASCII and Unicode, that can be used to compose strings. Youll learn how strings are represented and manipulated using the legacy functions from the C Standard Library, the bounds-checking interfaces, and POSIX and Windows APIs.
Chapter 8: Input/Output This chapter will teach you how to perform input/output (I/O) operations to read data from, or write data to, terminals and filesystems. I/O involves all the ways information enters or exits a program, without which your programs would be useless. Well cover techniques that make use of C Standard streams and POSIX file descriptors.
Chapter 9: Preprocessor Youll learn how to use the preprocessor to include files, define object- and function-like macros, and conditionally include code based on implementation-specific features.
Chapter 10: Program Structure Youll learn how to structure your program into multiple translation units consisting of both source and include files. Youll also learn how to link multiple object files together to create libraries and executable files.
Chapter 11: Debugging, Testing, and Analysis This chapter describes tools and techniques for producing correct programs, including compiletime and runtime assertions, debugging, testing, static analysis, and dynamic analysis. The chapter also discusses which compiler flags are recommended for use in different phases of the software development process.
Youre about to embark on a journey from which you will emerge a newly minted but professional C developer