Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Beginning Programming with C For Dummies®
Nội dung xem thử
Mô tả chi tiết
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 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 printon-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-11873763-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
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
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
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
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
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
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
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
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
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
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
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