Copyright 2017. Wolfram Donat. All rights reserved.
Published by
Maker Media, Inc.
1700 Montgomery Street, Suite 240
San Francisco, CA 94111
Maker Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com.
Publisher: Roger Stewart
Editor: Patrick DiJusto
Copy Editor and Proofreader: Elizabeth Welch, Happenstance Type-O-Rama
Interior Designer and Compositor: Maureen Forys, Happenstance Type-O-Rama
Cover Designer: Maureen Forys, Happenstance Type-O-Rama
December 2017: First Edition
Revision History for the First Edition
2017-12-13 First Release
See oreilly.com/catalog/errata.csp?isbn=9781680454284 for release details.
Make:, Maker Shed, and Maker Faire are registered trademarks of Maker Media, Inc. The Maker Media logo is a trademark of Maker Media, Inc. Jumpstarting C and related trade dress are trademarks of Maker Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Maker Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-68045-428-4
Safari Books Online
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the worlds leading authors in technology and business. Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training. Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals. Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like OReilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.
How to Contact Us
Please address comments and questions to the publisher:
Maker Media, Inc.
1700 Montgomery Street, Suite 240
San Francisco, CA 94111
You can send comments and questions to us by email at books@makermedia.com.
Maker Media unites, inspires, informs, and entertains a growing community of resourceful people who undertake amazing projects in their backyards, basements, and garages. Maker Media celebrates your right to tweak, hack, and bend any Technology to your will. The Maker Media audience continues to be a growing culture and community that believes in bettering ourselves, our environment, our educational systemour entire world. This is much more than an audience, its a worldwide movement that Maker Media is leading. We call it the Maker Movement.
To learn more about Make: visit us at makezine.com. You can learn more about the company at the following websites:
Maker Media: makermedia.com
Maker Faire: makerfaire.com
Maker Shed: makershed.com
Maker Share: makershare.com
Preface
A sk almost any computer hardware or software engineer which programming language she learned first, and the answer is bound to be C or C++. Many computer scientists start with Java or Cs stepchild, C#, but engineers normally learn C. Its the best way, after all, to get into the basics of algorithms and operating systems. The Windows, Mac, and Linux kernels, after all, are all written mostly in C. Aside from assembly, its probably one of the most basic programming languages.
C is powerful, fast, and unsexy. Its got built-in errors you may have never heard of (What the hell is a segmentation fault?!) and lets you mess with actual memory heaps and registers. There are no classes or even stringsyoure dealing with character arrays here, folks. Theres no fancy garbage collection if you use some memory for an array, youll probably want to free up that memory when youre done, or youll have a memory leak that could crash your program later.
C is cross-platform, but not in the same way that Python is cross-platform. Python code written on a Linux box can, with a few exceptions, run on a Windows machine without alterations. Most C written on a Linux computer will run on a Windows computer, but it will need to be recompiled first. C executables are specific to the machine on which theyre compiled, and transferring between operating systems can pose some unique challenges.
If you take the time to learn C (and its big brother, C++), your programming will benefit, even if youre already an experienced Python, Java, or even web programmer. Programming in C brings you back to basics, doesnt let you cut corners, and truly makes you think about what youre trying to do and the best way to do it. Yes, it can be incredibly frustratingI have lost literally hours trying to debug a piece of code, only to find it was merely missing one semicolonbut it can also be quite rewarding. Theres something about programming close to the metal that reminds me why I became a computer engineer in the first place.
Im assuming that you, the reader, bring the following to this book:
- A computer, and the ability to access the Internet
- Some knowledge of programming, even very basic
- A Raspberry Pi Zero or Zero W and all assorted cables
- Some LEDs, resistors, a breadboard, and some jumper wires
So if youre wanting to expand your horizons, and learn some new languages and some new skills, follow along and Ill teach you some C!
Chapter 1
Installing the Required Bits and Pieces
I n this chapter, youll learn about C and C++ IDEs and compilers, and Ill walk you through the process of installing them on your favorite computer. Be warned, however, that the projects in chapters 3 and 4 will be running on a Raspberry Pi Zero, which is a Linux-based system.
If you already have a Pi Zero or another Linux machine, I recommend you set it up now. The basic programs that well go through in chapters 1 and 2 will run on any system, but if youre already familiar with Linux and the Pi, so much the better.
If youd like to get started learning with Linux but dont have a computer on which to install it, I suggest you check out VirtualBox (http://www.virtualbox.org) from Oracle. VirtualBox is free and lets you install almost any client operating system (Windows, Linux, and a few others) onto a virtual computer running inside your host machine (Windows, Mac, or Linux). Since everything is free, you can download VirtualBox for your current computer, download a free Linux version (Ubuntu is probably the most popular, but you have many to choose from), start up your new virtual computer, and follow along with this book.
Next page