Author:Gookin, Dan
No description
Tags
Support Statistics
¥.00 ·
0times
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
Beginning Programming with C For Dummies® Published byJohn Wiley & Sons, Inc.111 River StreetHoboken, NJ 07030-5774 www.wiley.com Copyright © 2014 by John Wiley & Sons, Inc., Hoboken, New Jersey Published 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 either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. 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, the Wiley logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, 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
Page
4
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 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 www.wiley.com/techsupport. Wiley also publishes its books in a variety of electronic formats and by print-on-demand. Not all content that is available in standard print versions of this book may appear or be packaged in all book formats. If you have purchased a version of this book that did not include media that is referenced by or accompanies a standard print version, you may request this media by visiting http://booksupport.wiley.com. For more information about Wiley products, visit us at www.wiley.com. Library of Congress Control Number: 2013948031 ISBN 978-1-118-73763-7 (pbk); ISBN 978-1-118-73765-1 (ebk); ISBN 978-1-118-73762-0 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1
Page
5
Beginning Programming with C For Dummies® Visit www.dummies.com/cheatsheet/beginningprogrammingwithc to view this book's cheat sheet. Table of Contents Introduction Is the C Language Relevant? The C Programming For Dummies Approach How This Book Works Icons Used in This Book Parting Thoughts Part I: Getting Started with C Programming Chapter 1: A Quick Start for the Impatient What You Need in Order to Program Obtaining programming tools Acquiring an Integrated Development Environment (IDE) Behold the Code::Blocks IDE Installing Code::Blocks Touring the Code::Blocks workspace Your First Project
Page
6
Creating a new project Examining the source code Building and running the project Saving and closing Chapter 2: The Programming Thing The History of Programming Reviewing early programming history Introducing the C language The Programming Process Understanding programming Writing source code Compiling to object code Linking in the C library Running and testing Chapter 3: Anatomy of C Parts of the C Language Keywords Functions Operators Variables and values Statements and structure Comments Behold the Typical C Program Understanding C program structure
Page
7
Setting the main() function Returning something to the operating system Adding a function Part II: C Programming 101 Chapter 4: Trials and Errors Display Stuff on the Screen Displaying a humorous message Introducing the puts() function Adding more text Commenting out a statement Goofing up on purpose More Display Nonsense Displaying text with printf() Introducing the printf() function Understanding the newline Employing escape sequences Goofing up on purpose again Chapter 5: Values and Constants A Venue for Various Values Understanding values Displaying values with printf() Minding the extra zeroes The Computer Does the Math
Page
8
Doing simple arithmetic Reviewing the float-integer thing Always the Same Using the same value over and over Introducing constants Putting constants to use Chapter 6: A Place to Put Stuff Values That Vary Setting up a quick example Introducing the variable types Using variables Variable Madness! Using more-specific variable types Creating multiple variables Assigning a value upon creation Reusing variables Chapter 7: Input and Output Character I/O Understanding input and output devices Fetching characters with getchar() Using the putchar() function Working with character variables Text I/O, but Mostly I Storing strings
Page
9
Introducing the scanf() function Reading a string with scanf() Reading values with scanf() Using fgets() for text input Chapter 8: Decision Making If What? Making a simple comparison Introducing the if keyword Comparing values in various ways Knowing the difference between = and == Forgetting where to put the semicolon Multiple Decisions Making more-complex decisions Adding a third option Multiple Comparisons with Logic Building a logical comparison Adding some logical operators The Old Switch Case Trick Making a multiple-choice selection Understanding the switch-case structure Taking no breaks The Weird ?: Decision Thing Chapter 9: Loops, Loops, Loops A Little Déjà Vu
Page
10
The Thrill of for Loops Doing something x number of times Introducing the for loop Counting with the for statement Looping letters Nesting for loops The Joy of the while Loop Structuring a while loop Using the do-while loop Loopy Stuff Looping endlessly Looping endlessly but on purpose Breaking out of a loop Screwing up a loop Chapter 10: Fun with Functions Anatomy of a Function Constructing a function Prototyping (or not) Functions and Variables Using variables in functions Sending a value to a function Sending multiple values to a function Creating functions that return values Returning early
Page
11
Part III: Build Upon What You Know Chapter 11: The Unavoidable Math Chapter Math Operators from Beyond Infinity Incrementing and decrementing Prefixing the ++ and -- operators Discovering the remainder (modulus) Saving time with assignment operators Math Function Mania Exploring some common math functions Suffering through trigonometry It’s Totally Random Spewing random numbers Making the numbers more random The Holy Order of Precedence Getting the order correct Forcing order with parentheses Chapter 12: Give Me Arrays Behold the Array Avoiding arrays Understanding arrays Initializing an array Playing with character arrays (strings) Working with empty char arrays
Page
12
Sorting arrays Multidimensional Arrays Making a two-dimensional array Going crazy with three-dimensional arrays Declaring an initialized multidimensional array Arrays and Functions Passing an array to a function Returning an array from a function Chapter 13: Fun with Text Character Manipulation Functions Introducing the CTYPEs Testing characters Changing characters String Functions Galore Reviewing string functions Comparing text Building strings Fun with printf() Formatting Formatting floating point Setting the output width Aligning output Gently Down the Stream Demonstrating stream input Dealing with stream input
Page
13
Chapter 14: Structures, the Multivariable Hello, Structure Introducing the multivariable Understanding struct Filling a structure Making an array of structures Weird Structure Concepts Putting structures within structures Passing a structure to a function Chapter 15: Life at the Command Prompt Conjure a Terminal Window Starting a terminal window Running code in Text mode The main() Function Arguments Reading the command line Understanding main()’s arguments Time to Bail Quitting the program Running another program Chapter 16: Variable Nonsense Variable Control Typecasting into disbelief Creating new things with typedef
Page
14
Making static variables Variables, Variables Everywhere Using global variables Creating a global structure variable Chapter 17: Binary Mania Binary Basics Understanding binary Displaying binary values Bit Manipulation Using the bitwise | operator Using bitwise & Operating exclusively with XOR Understanding the ~ and ! operators Shifting binary values Explaining the binbin() function The Joy of Hex Part IV: The Advanced Part Chapter 18: Introduction to Pointers The Biggest Problem with Pointers Sizing Up Variable Storage Understanding variable storage Reading a variable’s size Checking a variable’s location
Page
15
Reviewing variable storage info The Hideously Complex Topic of Pointers Introducing the pointer Working with pointers Chapter 19: Deep into Pointer Land Pointers and Arrays Getting the address of an array Working pointer math in an array Substituting pointers for array notation Strings Are Pointer-Things Using pointers to display a string Declaring a string by using a pointer Building an array of pointers Sorting strings Pointers in Functions Passing a pointer to a function Returning a pointer from a function Chapter 20: Linked Lists Give Me Memory! Introducing the malloc() function Creating string storage Freeing memory Lists That Link Allocating space for a structure
Page
16
Creating a linked list Editing a linked list Saving a linked list Chapter 21: It’s About Time What Time Is It? Understanding the calendar Working with time in C Time to Program Checking the clock Viewing a timestamp Slicing through the time string Snoozing Part V: And the Rest of It Chapter 22: Permanent Storage Functions Sequential File Access Understanding C file access Writing text to a file Reading text from a file Appending text to a file Writing binary data Working with binary data files Random File Access Writing a structure to a file
Page
17
Reading and rewinding Finding a specific record Saving a linked list to a file Chapter 23: File Management Directory Madness Calling up a directory Gathering more file info Separating files from directories Exploring the directory tree Fun with Files Renaming a file Copying a file Deleting a file Chapter 24: Beyond Mere Mortal Projects The Multi-Module Monster Linking two source code files Sharing variables between modules Creating a custom header file Other Libraries to Link Chapter 25: Out, Bugs! Code::Blocks Debugger Debugging setup Working the debugger
Page
18
Setting a breakpoint Watching variables Solving Problems by Using printf() and puts() Documenting problems Saving comments for future-you Improved Error Messages Part VI: The Part of Tens Chapter 26: Ten Common Boo-Boos Conditional Foul-Ups == v. = Dangerous Loop Semicolons Commas in for Loops Missing break in a Switch Structure Missing Parentheses and Curly Brackets Pay Heed to That 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
Page
19
Break Apart Larger Projects into Several Modules Know What a Pointer Is Add White Space before Condensing Know When if-else Becomes switch-case Remember Assignment Operators When You Get Stuck, Read Your Code Out Loud Afterword Appendix A: ASCII Codes Appendix B: Keywords Appendix C: Operators Appendix D: Variable Types Appendix E: Escape Sequences Appendix F: Conversion Characters Conversion-character formatting Appendix G: Order of Precedence About the Author Cheat Sheet Connect with Dummies
Page
20
Introduction Say “Hello, world” to Beginning Programming with C For Dummies, a book that transforms you from a well-meaning, caring human being into an admired element of the underground nerd subculture, the programmer. Oh, yes, that’s a good thing. When you learn to code in C, you become the ultimate master of a number of electronic gizmos. You can craft your own programs, dictating to computers, tablets, and cell phones your very whims and desires. And 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. Yes, 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. Is the C Language Relevant? 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. In a way, C is 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 could 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
Comments 0
Loading comments...
Reply to Comment
Edit Comment