Sams Teach Yourself C in 24 Hours (2 page)

BOOK: Sams Teach Yourself C in 24 Hours
11.59Mb size Format: txt, pdf, ePub
ads

Statements ........................................................................................................45

Statement Blocks ..............................................................................................45

Anatomy of a C Function ......................................................................................46

Determining a Function’s Type ........................................................................46

Giving a Function a Valid Name ......................................................................47

Passing Arguments to C Functions ..................................................................47

The Beginning and End of a Function ............................................................48

The Function Body ..........................................................................................48

Making Function Calls ..........................................................................................49

Summary ................................................................................................................51

Q&A ......................................................................................................................52

Workshop ..............................................................................................................52

Quiz ..................................................................................................................52

Exercises ..........................................................................................................53

Hour 4 Understanding Data Types and Keywords

55

C Keywords ..........................................................................................................56

The char Data Type ..............................................................................................57

Character Variables ..........................................................................................58

Character Constants..........................................................................................58

The Escape Character (\) ..................................................................................59

Printing Characters ..........................................................................................60

The int Data Type ................................................................................................62

Declaring Integer Variables ..............................................................................62

Showing the Numeric Values of Characters ....................................................63

The float Data Type ............................................................................................64

Declaring Floating-Point Variables ..................................................................64

The Floating-Point Format Specifier (%f) ........................................................65

The double Data Type ..........................................................................................67

Using Scientific Notation ......................................................................................67

Naming a Variable..................................................................................................68

Summary ................................................................................................................68

Q&A ......................................................................................................................68

00 067231861x FM 4.10.2000 10:53 AM Page vii

Contents

vii

Workshop ..............................................................................................................69

Quiz ..................................................................................................................69

Exercises ..........................................................................................................70

Hour 5 Handling Standard Input and Output

71

Understanding Standard I/O ................................................................................72

Getting Input from the User ..................................................................................72

Using the getc() Function ..............................................................................72

Using the getchar() Function ........................................................................74

Printing Output on the Screen ..............................................................................75

Using the putc() Function ..............................................................................75

Another Function for Writing: putchar() ......................................................77

Revisiting the printf() Function..........................................................................78

Converting to Hex Numbers ............................................................................79

Specifying the Minimum Field Width..............................................................81

Aligning Output................................................................................................83

Using the Precision Specifier ..........................................................................84

Summary ................................................................................................................85

Q&A ......................................................................................................................86

Workshop ..............................................................................................................86

Quiz ..................................................................................................................87

Exercises ..........................................................................................................87

Part II Operators and Control-flow Statements

89

Hour 6 Manipulating Data

91

Arithmetic Assignment Operators ........................................................................92

The Assignment Operator (=) ..........................................................................92

Combining Arithmetic Operators with = ..........................................................92

Getting Negations of Numeric Values ..................................................................95

Incrementing or Decrementing by One ................................................................96

Greater Than or Less Than? ..................................................................................98

Using the Cast Operator ......................................................................................101

Summary ..............................................................................................................102

Q&A ....................................................................................................................102

Workshop ............................................................................................................103

Quiz ................................................................................................................103

Exercises ........................................................................................................103

Hour 7 Working with Loops

105

The while Loop ..................................................................................................106

The do-while Loop..............................................................................................107

Looping Under the for Statement ......................................................................109

00 067231861x FM 4.10.2000 10:53 AM Page viii

viii

Sams Teach Yourself C in 24 Hours

The Null Statement ..............................................................................................112

Using Complex Expressions in a for Statement............................................113

Using Nested Loops ............................................................................................116

Summary ..............................................................................................................118

Q&A ....................................................................................................................118

Workshop ............................................................................................................119

Quiz ................................................................................................................119

Exercises ........................................................................................................120

Hour 8 Using Conditional Operators

121

Measuring Data Sizes ..........................................................................................122

Everything Is Logical ..........................................................................................124

The Logical AND Operator (&&) ....................................................................124

The Logical OR Operator (||) ......................................................................126

The Logical NEGATION Operator (!) ..........................................................128

Manipulating Bits ................................................................................................129

Converting Decimal to Hex or Binary ..........................................................129

Using Bitwise Operators ................................................................................130

Using Shift Operators ....................................................................................133

What Does x?y:z Mean? ....................................................................................135

Summary ..............................................................................................................137

Q&A ....................................................................................................................137

Workshop ............................................................................................................138

Quiz ................................................................................................................138

Exercises ........................................................................................................138

Hour 9 Working with Data Modifiers and Math Functions

141

Enabling or Disabling the Sign Bit......................................................................142

The signed Modifier ......................................................................................142

The unsigned Modifier ..................................................................................143

Changing Data Sizes............................................................................................145

The short Modifier ........................................................................................145

The long Modifier ..........................................................................................145

Adding h, l, or L to printf and fprintf Format Specifiers..........................147

Mathematical Functions in C ..............................................................................148

Calling sin(), cos(), and tan() ....................................................................149

Calling pow() and sqrt() ..............................................................................150

Summary ..............................................................................................................152

Q&A ....................................................................................................................153

Workshop ............................................................................................................154

Quiz ................................................................................................................154

Exercises ........................................................................................................154

00 067231861x FM 4.10.2000 10:53 AM Page ix

Contents

ix

Hour 10 Controlling Program Flow

155

Always Saying “if…” ..........................................................................................156

The if-else Statement ..................................................................................158

Nested if Statements......................................................................................160

The switch Statement..........................................................................................161

The break Statement............................................................................................164

Breaking an Infinite Loop....................................................................................166

The continue Statement ......................................................................................167

The goto Statement..............................................................................................168

Summary ..............................................................................................................170

Q&A ....................................................................................................................170

BOOK: Sams Teach Yourself C in 24 Hours
11.59Mb size Format: txt, pdf, ePub
ads

Other books

Lilies That Fester by Janis Harrison
A Woman in the Crossfire by Samar Yazbek
The Bloodlust by L. J. Smith
Pole Dance by J. A. Hornbuckle
Loving Ms. Wrong by Red Hot Publishing
Baby, Come Back by Erica Spindler