Contents in Detail
EFFECTIVE C
An Introduction to Professional C Programming
by Robert C. Seacord
San Francisco
EFFECTIVE C. Copyright 2020 by Robert C. Seacord.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-10: 1-7185-0104-8
ISBN-13: 978-1-71850-104-1
Publisher: William Pollock
Executive Editor: Barbara Yien
Production Manager: Laurel Chun
Production Editor: Katrina Taylor
Cover Illustration: Gina Redman
Interior Design: Octopod Studios
Project Editor: Dapinder Dosanjh
Developmental Editors: Liz Chadwick and Frances Saux
Technical Reviewer: Martin Sebor
Copyeditor: Sharon Wilkey
Compositor: Happenstance Type-O-Rama
Proofreader: Emelie Battaglia
Indexer: Chelsea and Drew Hoffman
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900;
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Names: Seacord, Robert C., author.
Title: Effective C : an introduction to professional C programming / Robert
C. Seacord.
Description: San Francisco : No Starch Press, Inc., 2020. | Includes
bibliographical references and index. | Summary: An in-depth guide for
developers who already know how to program and want to quickly come up
to speed on professional programming in C. It introduces the languages
basic data types and code structures ; addresses best practices and
common errors ; and teaches how to debug, test, and analyze C programs
Provided by publisher.
Identifiers: LCCN 2020017146 (print) | LCCN 2020017147 (ebook) | ISBN
9781718501041 (paperback) | ISBN 1718501048 (paperback) | ISBN
9781718501058 (ebook)
Subjects: LCSH: C (Computer program language)
Classification: LCC QA76.73.C15 S417 2020 (print) | LCC QA76.73.C15
(ebook) | DDC 005.13/3--dc23
LC record available at https://lccn.loc.gov/2020017146
LC ebook record available at https://lccn.loc.gov/2020017147
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
Dedicated to my granddaughters, Olivia and
Isabella, and to other young women who will
grow up to be scientists and engineers
About the Author
Robert Seacord () is a Technical Director at NCC Group, where he develops and delivers secure coding training in C, C++, and other languages. Robert is also an expert on ISO/IEC JTC1/SC22/WG14, the international standardization working group for the C programming language. He is the author of other books, including The CERT C Coding Standard, Second Edition (Addison-Wesley, 2014), Secure Coding in C and C++, Second Edition (Addison-Wesley, 2013), and Java Coding Guidelines: 75 Recommendations for Reliable and Secure Programs (Addison-Wesley, 2014). He has also published more than 50 papers on software security, component-based software engineering, web-based system design, legacy-system modernization, component repositories and search engines, and user interface design and development.
About the Contributor
Aaron Ballman () is a compiler frontend engineer for GrammaTech, Inc. and works primarily on the static analysis tool CodeSonar. He is also a frontend maintainer for Clang, a popular open source compiler for C, C++, and other languages. Aaron is an expert for the JTC1/SC22/WG14 C programming language and JTC1/SC22/WG21 C++ programming language standards committees. His primary professional focus has been in helping programmers recognize mistakes in their code through better language design, diagnostics, and tooling. When not thinking about programming, he enjoys spending quiet moments in the woods of rural Maine with his family.
About the Tech Reviewer
Martin Sebor is Principal Software Engineer with the GNU Toolchain Team at Red Hat. His main focus is the GCC compiler and, in particular, the areas of detecting, diagnosing, preventing security-related issues in C and C++ programs, and implementing optimizations of string-based algorithms. Prior to joining Red Hat in 2015, he worked as a compiler toolchain engineer at Cisco. Martin has been a member of the C++ standardization committee since 1999 and a member of the C language committee since 2010. He lives with his wife near the small town of Lyons, Colorado.
CONTENTS IN DETAIL
1
GETTING STARTED WITH C
2
OBJECTS, FUNCTIONS, AND TYPES
3
ARITHMETIC TYPES
4
EXPRESSIONS AND OPERATORS
5
CONTROL FLOW
6
DYNAMICALLY ALLOCATED MEMORY
7
CHARACTERS AND STRINGS
8
INPUT/OUTPUT
9
PREPROCESSOR
10
PROGRAM STRUCTURE
11
DEBUGGING, TESTING, AND ANALYSIS
FOREWORD
BY PASCAL CUOQ
The first time I came across Robert Seacords name was in 2008. Robert was already well known in the C programming galaxy for his work on the CERT C Coding Standard and Annex K of the C Standard. But in 2008, it had been only a few years sinceyoung and foolishI had embarked on the Frama-C project to guarantee the absence of undefined behavior in C programs. At some point, a CERT Vulnerability Note about how C compilers (and GCC in particular) removed certain pointer-arithmetic overflow checks, piqued my interest. The compilers had reason to eliminate the checks: naively written, they invoked undefined behavior when the overflow was present.
The C compilers were also allowed to tell the programmer nothing about what they had done wrong, even at the maximum warning level. Undefined behavior in C can be harsh. I had set out to solve this exact problem. Robert was one of the authors of that note.
Effective C will teach you C programming for the modern era. It will help you establish good habits to keep you from using undefined behavior, whether voluntarily or through negligence. Let the reader be warned: in large C programs, avoiding ordinary programming errors alone may not suffice for dodging undefined behavior caused by arbitrary inputs!
This books emphasis on the security aspects of C programming is unmatched. My personal recommendation is that, after reading it, you use all of the available tools it presents to avoid undefined behavior in the C programs you write.
Pascal Cuoq
Chief Scientist, TrustInSoft