C Programming For Dummies (Dan Gookin) (Z-Library)

Author: Dan Gookin

商业

As with any major language, mastery of C can take you to some very interesting new places. Almost 50 years after it first appeared, it’s still the world’s most popular programming language and is used as the basis of global industry’s core systems, including operating systems, high-performance graphics applications, and microcontrollers. This means that fluent C users are in big demand at the sharp end in cutting-edge industries—such as gaming, app development, telecommunications, engineering, and even animation—to translate innovative ideas into a smoothly functioning reality. To help you get to where you want to go with C, this 2nd edition of C Programming For Dummies covers everything you need to begin writing...

📄 File Format: PDF
💾 File Size: 5.2 MB
50
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (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
C Programming For Dummies®, 2nd Edition Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2021 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL
📄 Page 4
ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print- on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2020945155 ISBN: 978-1-119-74024-7; 978-1-119-74025-4 (ebk); 978-1-119-74026- 1 (ebk)
📄 Page 5
C Programming For Dummies® To view this book's Cheat Sheet, simply go to www.dummies.com and search for “C Programming For Dummies Cheat Sheet” in the Search box. Table of Contents Cover Title Page Copyright Page Introduction Why the C Language? The C Programming For Dummies Approach How This Book Works Icons Used in This Book Parting Thoughts Part 1: The ABs of C Chapter 1: A Quick Start for the Impatient What You Need to Program Command Prompt Programming IDE Programming Your First Program Chapter 2: The Programming Thing The History of Programming The Programming Process
📄 Page 6
Chapter 3: Anatomy of C Parts of the C Language Behold the Typical C Program Part 2: C Programming 101 Chapter 4: Trials and Errors Display Stuff on the Screen More Text Output Nonsense Chapter 5: Values and Simple Math A Venue for Various Values The Computer Does the Math Chapter 6: A Place to Put Stuff Values That Vary Variable Madness! Constants Always the Same Chapter 7: Input and Output Character I/O Text I/O, but Mostly I Chapter 8: Decision Making What If? Multiple Decisions Multiple Comparisons with Logic The Old Switch Case Trick The Weird ?: Decision Thing Chapter 9: Loops, Loops, Loops A Little Déjà Vu The Thrill of for Loops The Joy of the while Loop Loopy Stuff Chapter 10: Fun with Functions Anatomy of a Function Functions and Variables
📄 Page 7
Constants of the Global Kind Part 3: Build Upon What You Know Chapter 11: The Unavoidable Math Chapter Math Operators from Beyond Infinity Math Function Mania It’s Totally Random The Holy Order of Precedence Chapter 12: Give Me Arrays Behold the Array Multidimensional Arrays Arrays and Functions Chapter 13: Fun with Text Character Manipulation Functions String Functions Galore Fun with printf() Formatting Gently Down the Stream Chapter 14: Structures, the Multivariable Hello, Structure Weird Structure Concepts Chapter 15: Life at the Command Prompt Conjure a Terminal Window Arguments for the main() Function Time to Bail Chapter 16: Variable Nonsense Variable Control Variables, Variables Everywhere Chapter 17: Binary Mania Binary Basics Bit Manipulation The Joy of Hex Part 4: The Advanced Part
📄 Page 8
Chapter 18: Introduction to Pointers The Biggest Problem with Pointers Sizing Up Variable Storage The Hideously Complex Topic of Pointers Chapter 19: Deep into Pointer Land Pointers and Arrays Strings Are Pointer-Things Pointers in Functions Chapter 20: Memory Chunks and Linked Lists Give Me Memory! Lists That Link Chapter 21: It’s About Time What Time Is It? Time to Program Part 5: And the Rest of It Chapter 22: Permanent Storage Functions Sequential File Access Random File Access Chapter 23: File Management Directory Madness Fun with Files Chapter 24: Beyond Mere Mortal Projects The Multi-Module Monster Other Libraries to Link Chapter 25: Out, Bugs! Simple Tricks to Resolve Problems The Debugger Improved Error Messages Part 6: The Part of Tens Chapter 26: Ten Common Boo-Boos Conditional Foul-Ups
📄 Page 9
== v. = Dangerous Loop Semicolons Commas in for Loops Missing break in a switch Structure Missing Parentheses and Curly Brackets Don’t Ignore a Warning Endless Loops scanf() Blunders Streaming Input Restrictions Chapter 27: Ten Reminders and Suggestions Maintain Good Posture Use Creative Names Write a Function Work on Your Code a Little Bit at a Time Break Apart Larger Projects into Several Modules Know What a Pointer Is Add Whitespace before Condensing Know When if-else Becomes switch-case Remember Assignment Operators When You Get Stuck, Read Your Code Out Loud Part 7: Appendices Appendix A: ASCII Codes Appendix B: Keywords Appendix C: Operators Appendix D: Data Types Appendix E: Escape Sequences Appendix F: Conversion Characters Appendix G: Order of Precedence Index About the Author
📄 Page 10
Connect with Dummies End User License Agreement List of Tables Chapter 3 TABLE 3-1 C Language Keywords Chapter 4 TABLE 4-1: Escape Sequences Chapter 5 TABLE 5-1: Basic Math Operators Chapter 6 TABLE 6-1 Basic C Language Variable Types TABLE 6-2 More C Language Data Types Chapter 8 TABLE 8-1 C Language Comparison Operators TABLE 8-2 Logical Comparison Operators Chapter 11 TABLE 11-1 C Math Operators TABLE 11-2 C Math Assignment Operators TABLE 11-3 Common, Sane Math Functions Chapter 13 TABLE 13-1 CTYPE Testing Functions TABLE 13-2 CTYPE Manipulation Functions TABLE 13-3 String Functions Chapter 17 TABLE 17-1 Binary Groupings TABLE 17-2 Powers of 2 TABLE 17-3 Binary Operators TABLE 17-4 Hexadecimal Values
📄 Page 11
Chapter 19 TABLE 19-1 Pointers and Peekers In and Out of Parentheses TABLE 19-2 Array Notation Replaced by Pointers TABLE 19-3 Pointer Notation and Array Notation Chapter 22 TABLE 22-1: Access Modes for the fopen() Function Appendix G TABLE G-1 Standard Operator Precedence TABLE G-2 Pointers and Precedence List of Illustrations Chapter 1 FIGURE 1-1: The Code::Blocks workspace. FIGURE 1-2: Program output. Chapter 5 FIGURE 5-1: Matching printf() conversion characters and arguments. Chapter 11 FIGURE 11-1: Degrees and radians. Chapter 12 FIGURE 12-1: Storing strings in a two-dimensional array. Chapter 15 FIGURE 15-1: Text mode in a terminal window. FIGURE 15-2: Setting command-line arguments in Code::Blocks. Chapter 17 FIGURE 17-1: Base 2 values in a byte. Chapter 18 FIGURE 18-1: How a structure fits in memory. FIGURE 18-2: Variable locations in memory. FIGURE 18-3: Using a pointer to read values.
📄 Page 12
Chapter 19 FIGURE 19-1: Filling an array by using a pointer. FIGURE 19-2: How the ** thing works. Chapter 20 FIGURE 20-1: A linked list in memory. FIGURE 20-2: Removing an item from a linked list. FIGURE 20-3: Adding an item to a linked list. Chapter 24 FIGURE 24-1: Two source code files in the Code::Blocks IDE. Chapter 25 FIGURE 25-1: The Compiler toolbar. FIGURE 25-2: A breakpoint in the code. FIGURE 25-3: Monitoring variable values.
📄 Page 13
Introduction When I was in school, I'd open a new math textbook and look in the back, marveling at the problems. Someday, I thought, I would understand all this nonsense. You should do that with this book right now: Open it up to one of the final chapters. Look over the C programming code and think to yourself, “Someday soon, this will all make perfect sense to me.” Say “Hello, world” to C Programming For Dummies, 2nd Edition, the book that sets you on the path to become a computer programmer. Once despised vermin, banished to basement server rooms and suffering from a lack of personal hygiene, programmers are now valued and contributing members of society. Some are billionaires. And they all started their careers by learning to program. The C language lets you master a number of electronic gizmos. You can craft your own programs, dictating your every whim and desire to computers, tablets, and cell phones. The electronics dutifully obey. Given the information offered in this book, you can pass that programming class, impress your friends, be admired by Hollywood, or even start your own software company. Truly, learning to program is a worthy investment of your time. This book helps make learning how to program understandable and enjoyable. You don’t need any programming experience — you don’t even need to buy new software. You just need the desire to program in C and the ability to have fun while doing so. Why the C Language? An argument surfaces every few years that learning C is a road to nowhere. Newer, better programming languages exist, and it’s far better to learn them than to waste time learning C. Poppycock.
📄 Page 14
C continues to dominate the charts for best and most useful programming languages, often beating out the newer languages the cool programmers use. Further, C is like the Latin of computer languages: Just about every Johnny-come-lately programming language uses C syntax. C keywords and even certain functions find their way into other popular languages, from C++ to Java to Python to whatever the latest, trendy language might be. My point is that once you learn C programming, all those other programming languages come easy. In fact, many of the books that teach those other languages often assume that you know a little C before you start out. This assumption is frustrating for a beginner — but not when you already know C. So ignore the lofty pundits and know-it-all poohbahs. C is still relevant. Programming for microcontrollers, operating systems, and major software packages is still done using good ol’ C. You are not wasting your time. The C Programming For Dummies Approach As a programmer, I’ve toiled through many programming books. I know what I really don’t like to see, and, lamentably, I see it often — that is, where the author writes pages-long code or boasts about what he knows, impressing his fellow nerds and not really teaching anything. Too much of this type of training exists, which is probably why you’ve picked up this book. My approach here is simple: Short programs. To-the-point demonstrations. Lots of examples. Plenty of exercises. The best way to learn something is by doing it. Each concept presented in this book is coupled with sample code. The listings are short enough that you can quickly type them in — and I recommend that you do so. You can then build and run the code to see how things work. This
📄 Page 15
immediate feedback is not only gratifying, it’s also a marvelous learning tool. Sample programs are followed by exercises you can try on your own, testing your skills and expanding your knowledge. Suggested answers to the exercises and all the source code can be found on this book’s companion website: https://c-for-dummies.com/cprog
📄 Page 16
How This Book Works This book teaches the C programming language. It starts out by assuming that you know little to nothing about programming, and it finishes by covering some of the more advanced C operations. To program in C, you need a computer. This book makes no assumptions about the computer you select: It can be a Windows PC, a Macintosh, or a Linux system. You can choose to use an integrated development environment (IDE) such as Code::Blocks, or you can compile and run the sample programs at a command prompt. This book also wastes no time, getting you started immediately in Chapter 1. Nothing is introduced without a full explanation first. Due to the nature of programming, I’ve made a few exceptions; they’re carefully noted in the text. Otherwise, the book flows from front to back, which is how best to read this book. C language keywords and functions are shown in italic text, as in printf() and break. Some keywords, such as for and if, may make the sentence read in a goofy way, which is why those words are shown in italic. Filenames and variable names are shown in monofont type, such as program.exe and loop. If you need to type something, that text is shown in bold. For example, “Type the blorfus command” means that you should type blorfus at the keyboard. You're directed when to press the Enter key, if at all. When working numbered steps, text to type appears in regular (roman) type: 3. Type exit and press the Enter key. You type the word exit and then press the Enter key. Program samples are shown as snippets on the page, similar to this one: if( i == 1) printf("eye won");
📄 Page 17
You don’t need to type an example unless you’re directed to do so. Full program listings are shown and numbered in each chapter; for example: LISTING 1-1 The Code::Blocks Skeleton #include <stdio.h> #include <stdlib.h> int main() { printf("Hello world!\n"); return(0); } Because of this book’s margins, text in a listing may occasionally wrap, extending from one line to the next. You do not need to split up your code in a similar manner, and I remind you whenever such a thing occurs. The listings in this book don’t contain line numbers, but your text editor might. This book references the sample code listings by using the line numbers, which you can also use in your editor to examine the code. Exercises are numbered by chapter and then sequentially. So the third exercise in Chapter 13 is Exercise 13-3. You’re directed in the text to work an exercise. Here’s an example: Exercise 1-1: Type the source code from Listing 1-1 into your editor. Save it under the filename ex0101.c. Build and run. Answers for all exercises can be found on the web: https://c-for-dummies.com/cprog Go to this web page if you want to copy-and-paste the source code as well. Icons Used in This Book
📄 Page 18
This icon flags information worthy enough to remember. Though I recommend remembering as much as you can, these icons flag the stuff you just can’t forget. A tip is a suggestion, a special trick, or something super fancy to help you out. This icon marks something you need to avoid. It’s advice that could also be flagged with a Tip or Remember icon but has dire consequences if ignored. Face it: All of programming is technical. I reserve the use of this icon for extra-technical tidbits, asides, and anecdotes. Call it “nerd stuff.” Parting Thoughts I enjoy programming. It’s a hobby, and I find it incredibly relaxing, frustrating, and rewarding. I assume that you share these feelings, though you may also be a struggling student or someone who wants a career. Regardless, enjoy programming. If you can imagine the program you want to write on a screen, you can make it happen. It may not happen as fast as you like, but it can happen. Please work the exercises in this book. Try some on your own, variations on a theme. Continue working at problems until you solve them. The
📄 Page 19
amazing thing about programming is that no single, absolutely correct way to do something exists. Anytime you try, you’re learning. If possible, find a programming friend who can help you. Don’t make them do the work or explain how things run, but rely on them as a resource. Programming can be a solo thing, but it’s good to occasionally commiserate with others who also program in C — or in any language. This book has a few companion websites. The primary one is found here: https://c-for-dummies.com/cprog You can also check out my C programming blog, which is updated every Saturday with new lessons and offers a monthly Exercise challenge: https://c-for-dummies.com/blog The publisher also features a companion website, which I’m obliged to mention here, though it’s not updated as frequently as my own site. Visit www.dummies.com and type C programming into the search box to find this book’s support page and other goodies. For more help, or just to say hi, you can send me email at dan@c-for-dummies.com I’m happy to hear from you, though I won’t write code for you. I also cannot explain university assignments. (I don’t do B-trees. No one does.) And if you have any questions specific to this book — especially any errors or typos — feel free to pass them along. Enjoy your C programming!
📄 Page 20
Part 1 The ABs of C
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List