Digital Library

Tiny C Projects (Dan Gookin)(Z-Library)

Share E-Book

Tiny C Projects (Dan Gookin)(Z-Library)

Author

Rating 3.5 AI initial rating

Log in to rate

C
Language English

Learn the big skills of C programming by creating bite-size projects! Work your way through these 15 fun and interesting tiny challenges to master essential C techniques you’ll use in full-size applications. In Tiny C Projects you will learn how to: • Create libraries of functions for handy use and re-use • Process input through an I/O filter to generate customized output • Use recursion to explore a directory tree and find duplicate files • Develop AI for playing simple games • Explore programming capabilities beyond the standard C library functions • Evaluate and grow the potential of your programs • Improve code to better serve users Tiny C Projects is an engaging collection of 15 small programming challenges! This fun read develops your C abilities with lighthearted games like tic-tac-toe, utilities like a useful calendar, and thought-provoking exercises like encoding and cyphers. Jokes and lighthearted humor make even complex ideas fun to learn. Each project is small enough to complete in a weekend, and encourages you to evolve your code, add new functions, and explore the full capabilities of C. About the technology The best way to gain programming skills is through hands-on projects—this book offers 15 of them. C is required knowledge for systems engineers, game developers, and roboticists, and you can start writing your own C programs today. Carefully selected projects cover all the core coding skills, including storing and modifying text, reading and writing files, searching your computer’s directory system, and much more. About the book Tiny C Projects teaches C gradually, from project to project. Covering a variety of interesting cases, from timesaving tools, simple games, directory utilities, and more, each program you write starts out simple and gets more interesting as you add features. Watch your tiny projects grow into real applications and improve your C skills, step by step.

Format PDF
Size 8.1 MB
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
(This page has no text content)
Page 2
(This page has no text content)
Page 3
Tiny C Projects DAN GOOKIN To comment go to liveBook Manning
Page 4
Shelter Island For more information on this and other Manning titles go to www.manning.com
Page 5
Copyright For online information and ordering of these and other Manning books, please visit www.manning.com. The publisher offers discounts on these books when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2022 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. ♾ Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to
Page 6
that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road Technical PO Box 761 Shelter Island, NY 11964 Development editor: Becky Whitney Technical development editor: Christopher Haupt Review editor: Aleksandar Dragosavljević Production editor: Andy Marinkovich Copy editor: Pamela Hunt Proofreader: Keri Hales Technical proofreader: Frances Buontempo Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781633439825
Page 7
contents front matter preface acknowledgments about this book about the author about the cover illustration 1 Configuration and setup 1.1 The C development cycle Editing source code Compiling, linking, building 1.2 The integrated development environment (IDE) Choosing an IDE Using Code::Blocks Using XCode 1.3 Command-line compiling Accessing the terminal window Reviewing basic shell commands Exploring text screen editors Using a GUI editor Compiling and running 1.4 Libraries and compiler options Linking libraries and setting other options in an IDE Using command-line compiler options 1.5 Quiz 2 Daily greetings
Page 8
2.1 The shell starts Understanding how the shell fits in Exploring various shell startup scripts Editing the shell startup script 2.2 A simple greeting Coding a greeting Adding a name as an argument 2.3 The time of day Obtaining the current time Mixing in the general time of day Adding specific time info 2.4 The current moon phase Observing moon phases Writing the moon phase algorithm Adding the moon phase to your greeting 2.5 A pithy saying Creating a pithy phrase repository Randomly reading a pithy phrase Adding the phrase to your greeting code 3 NATO output 3.1 The NATO alphabet 3.2 The NATO translator program Writing the NATO translator Reading and converting a file 3.3 From NATO to English Converting NATO input to character output Reading NATO input from a file 4 Caesarean cipher 4.1 I/O filters Understanding stream I/O
Page 9
Writing a simple filter Working a filter at the command prompt 4.2 On the front lines with Caesar Rotating 13 characters Devising a more Caesarean cipher 4.3 Deep into filter madness Building the hex output filter Creating a NATO filter Filtering words 5 Encoding and decoding 5.1 The concept of plain text Understanding ASCII Exploring the control codes Generating noncharacter output Playing with ASCII conversion tricks 5.2 The hex encoder/decoder Writing a simple hex encoder/decoder Coding a better hex encoder/decoder Adding a wee bit of error-checking 5.3 URL encoding Knowing all the URL encoding rules Writing a URL encoder Creating a URL decoder 6 Password generators 6.1 Password strategies Avoiding basic and useless passwords Adding password complexity Applying the word strategy 6.2 The complex password jumble Building a silly random password program Adding conditions to the password program
Page 10
Improving upon the password 6.3 Words in passwords Generating random words, Mad Libs style Building a random word password generator 7 String utilities 7.1 Strings in C Understanding the string Measuring a string Reviewing C string functions Returning versus modifying directly 7.2 String functions galore Changing case Reversing a string Trimming a string Splitting a string Inserting one string into another Counting words in a string Converting tabs to spaces 7.3 A string library Writing the library source and header file Creating a library Using the string library 7.4 A kinda OOP approach Adding a function to a structure Creating a string “object” 8 Unicode and wide characters 8.1 Text representation in computers Reviewing early text formats Evolving into ASCII text and code pages Diving into Unicode 8.2 Wide character programming
Page 11
Setting the locale Exploring character types Generating wide character output Receiving wide character input Working with wide characters in files 9 Hex dumper 9.1 Bytes and data Reviewing storage units and size mayhem Outputting byte values Dumping data 9.2 Dump that file! Reading file data Fixing uneven output 9.3 Command-line options Using the getopt() function Updating the dumpfile program code Setting abbreviated output Activating octal output 10 Directory tree 10.1 The filesystem 10.2 File and directory details Gathering file info Exploring file type and permissions Reading a directory 10.3 Subdirectory exploration Using directory exploration tools Diving into a subdirectory Mining deeper with recursion 10.4 A directory tree Pulling out the directory name Monitoring directory depth
Page 12
11 File finder 11.1 The great file hunt 11.2 A file finder Coding the Find File utility Understanding the glob Using wildcards to find files 11.3 The duplicate file finder Building a file list Locating the duplicates 12 Holiday detector 12.1 The operating system wants its vig Understanding exit status versus the termination status Setting a return value Interpreting the return value Using the preset return values 12.2 All about today Getting today’s date Obtaining any old date 12.3 Happy holidays Reviewing holidays in the United States Discovering holidays in the UK 12.4 Is today a holiday? Reporting regular date holidays Dealing with irregular holidays Calculating Easter Running the date gauntlet 13 Calendar 13.1 The calendar program 13.2 Good dates to know Creating constants and enumerating dates
Page 13
Finding the day of the week Calculating the first day of the month Identifying leap years Getting the time zone correct 13.3 Calendar utilities Generating a week Showing a month Displaying a full year Putting the full year into a grid 13.4 A calendar in color Understanding terminal colors Generating a tight-but-colorful calendar Coloring holidays 14 Lotto picks 14.1 A tax for those who are bad at math Playing the lottery Understanding the odds Programming the odds 14.2 Here are your winning numbers Generating random values Drawing lotto balls Avoiding repeated numbers, another approach 14.3 Never tell me the odds Creating the lotto() function Matching lottery picks Testing the odds 15 Tic-tac-toe 15.1 A silly kids’ game Playing tic-tac-toe Approaching the game mathematically 15.2 The basic game
Page 14
Creating the game grid Adding game play Limiting the input to free squares Determining the winner 15.3 The computer plays Choosing the number of players Coding a dumb opponent Adding some intelligence index
Page 15
front matter preface Is C programming still relevant? Every time I read that C is becoming obsolete, another article pops up on how C continues to be one of the most popular, in-demand programming languages—even as it passes its 50th birthday. Disparagement aside, C is the primary language used for system programming, networking, gaming, and coding microcontrollers. Even those trendy languages that the cool kids boast about most likely have their core originally written in C. It’s not going away any time soon. I often refer to C as the Latin of computer programming languages. Its syntax and even a few keywords are borrowed heavily by other languages. Just as knowing Latin helps you understand and learn French, Italian, Spanish, and other languages, knowing C allows you to easily understand and learn other programming languages. But don’t stop there! Honing your C skills is just as important as exercising a muscle. And what better way to work on and perfect your C programming abilities than to continually write small, useful programs?
Page 16
Why did I write this book? I feel the best way to learn programming is to use small demonstration programs. Each one focuses on a specific part of the language. The code is short and easy to type, and it drives home a point. If the little program can do something impressive, inspiring, or silly, all the better. My approach contrasts with other programming books I’ve read. These tedious tomes often list a single, huge program that drives home all the concepts. Typing 100 lines of code when you have no clue what’s going on is discouraging, and it misses one of the more delightful aspects of programming: instant feedback. Somehow, the habit of writing tiny programs sticks with me, even beyond when I’m writing a C programming book or teaching an online C programming course. For years, I’ve been coding tiny programs on my blog at https://c-for- dummies.com/blog. I do so to provide supplemental material for my readers and learners, but also because I enjoy coding. Of course, to make small programs meaningful, they must dwell in the ancient command-line, text-mode environment. Graphics are limited. Animation is dull. The excitement, however, remains—especially when something useful is presented all within only a few lines of code. My approach echoes the way I code: start small and grow the code. So, the programs in this book may begin as only a
Page 17
dozen lines of code that output a simple message. From there the process expands. Eventually a useful program emerges, all while remaining tiny and tight and teaching something useful along the way. Who knows when the mood will hit you and you decide to code a handy command-line utility to improve your workflow? With a knowledge of C programming, the desire, and a few hours of your time, you can make it happen. It’s my hope that this book provides you with ample inspiration. acknowledgments I set out to be a fiction author. At one point, I was engaged in personal correspondence with a magazine editor who liked my stuff, but nothing was ever published. Then along came a job at a computer book publishing house, CompuSoft. There I combined my self-taught skills in programming with my love of writing to help craft a series of technical books. It was there I learned how to write for beginners and inject humor in the text. Six years and 20 titles later, I wrote DOS For Dummies, which revolutionized the computer book publishing industry. This book showed that technological titles could successfully impart information to a beginner by using humor. The entire industry changed, and the For Dummies phenomenon continues to this day.
Page 18
Computer books have diminished as an industry, thanks to the internet and humanity’s disdain for reading printed material. Still, it’s been a great journey and I have many people to thank: Dave Waterman, for hiring me at CompuSoft and teaching me the basics of technical writing; Bill Gladstone and Matt Wagner, for being my agents; Mac McCarthy, for the insane idea of DOS For Dummies; and Becky Whitney, for being my long-time, favorite editor. She has taught me more about writing than anyone—or perhaps just taught me how to write in a way that makes her job as editor easy. I appreciate all of you. Finally, to all the reviewers: Adam Kalisz, Adhir Ramjiawan, Aditya Sharma, Alberto Simões, Ashley Eatly, Chris Kolosiwsky, Christian Sutton, Clifford Thurber, David Sims, Glen Sirakavit, Hugo Durana, Jean-François Morin, Jeff Lim, Joel Silva, Joe Tingsanchali, Juan Rufes, Jura Shikin, K. S. Ooi, Lewis Van Winkle, Louis Aloia, Maciej Jurkowski, Manu Raghavan Sareena, Marco Carnini, Michael Wall, Mike Baran, Nathan McKinley-Pace, Nitin Gode, Patrick Regan, Patrick Wanjau, Paul Silisteanu, Phillip Sorensen, Roman Zhuzha, Sanchir Kartiev, Shankar Swamy, Sriram Macharla, and Vitosh Doynov, your input helped make this a better book. about this book Who should read this book?
Page 19
This book assumes that you have a good knowledge of C. You don’t need to be an expert, but a raw beginner may struggle with the pace. Though I explain the techniques used and my approach for writing these small programs, I don’t go into detail regarding how the basic aspects of C work. The operating system I chose is Linux. Though I’ve run the code on a Linux box, I developed the programs on Ubuntu Linux running under Windows 10/11. The programs also run on a Macintosh. All the programs in this book are text mode, which requires a terminal window and knowledge of various shell commands, though nothing too technical or specific. Chapter 1 covers the details of coding and building in the command-prompt environment. Bottom line: this book was written for anyone who loves the C language, enjoys programming, and takes pleasure from writing small, useful, and interesting programs. How this book is organized: A road map This book is organized into 15 chapters. The first chapter touches upon configuration and setup to ensure that you get started properly and are able to code and create the programs without going nuts. Chapters 2 through 15 each cover a specific type of program. The chapter builds upon the program’s idea, often presenting a simple version and then expanding the program to offer more features. Sometimes other programs
Page 20
are introduced along the way, each of which follows the main theme or otherwise assists the primary program in its goal. Software/hardware requirements Any modern version of a C compiler works with this book. The code doesn’t touch upon any of the newer C language keywords. Some functions are specific to the GNU compiler. These are mentioned in the text, with alternative approaches available if your C compiler lacks the GNU extensions. No third-party libraries are required to build any of the programs. Variations in Linux distributions or between Windows 10/11 and macOS play no significant role in creating the code presented here. Online resources My personal C programming website is c-for-dummies.com, which is updated weekly. I’ve been keeping up my habit of weekly C language lessons since 2013, each one covering a specific topic in C programming, offering advice on coding techniques, and providing a monthly Exercise challenge. Please check out the blog for up-to-date information and feedback on C, as well as more details about this book. I also teach various C programming courses at LinkedIn Learning. These courses range from beginner level to advanced topics such as using various C language libraries,
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay

Every 10 yuan is 1 year of VIP; the remainder counts as months (5 yuan is 5 months, 100 yuan is 10 years). Signed-in users are enabled immediately and both you and the admin get an email. Guests receive an ID and are sent to register.

← Back to List