Sams Teach Yourself C in 24 Hours (5 page)

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

Hour 5, “Handling Standard Input and Output”..................................................447

Quiz ................................................................................................................447

Exercises ........................................................................................................447

Hour 6, “Manipulating Data” ..............................................................................449

Quiz ................................................................................................................449

Exercises ........................................................................................................449

Hour 7, “Working with Loops” ..........................................................................451

Quiz ................................................................................................................451

Exercises ........................................................................................................451

Hour 8, “Using Conditional Operators” ..............................................................453

Quiz ................................................................................................................453

Exercises ........................................................................................................453

Hour 9, “Working with Data Modifiers and Math Functions”............................455

Quiz ................................................................................................................455

Exercises ........................................................................................................456

Hour 10, “Controlling Program Flow” ................................................................458

Quiz ................................................................................................................458

Exercises ........................................................................................................458

00 067231861x FM 4.10.2000 10:53 AM Page xvi

xvi

Sams Teach Yourself C in 24 Hours

Hour 11, “Understanding Pointers” ....................................................................460

Quiz ................................................................................................................460

Exercises ........................................................................................................461

Hour 12, “Understanding Arrays” ......................................................................462

Quiz ................................................................................................................462

Exercises ........................................................................................................463

Hour 13, “Manipulating Strings” ........................................................................465

Quiz ................................................................................................................465

Exercises ........................................................................................................466

Hour 14, “Understanding Scope and Storage Classes” ......................................467

Quiz ................................................................................................................467

Exercises ........................................................................................................468

Hour 15, “Working with Functions”....................................................................470

Quiz ................................................................................................................470

Exercises ........................................................................................................470

Hour 16, “Applying Pointers”..............................................................................473

Quiz ................................................................................................................473

Exercises ........................................................................................................474

Hour 17, “Allocating Memory” ..........................................................................476

Quiz ................................................................................................................476

Exercises ........................................................................................................476

Hour 18, “Using Special Data Types and Functions” ........................................480

Quiz ................................................................................................................480

Exercises ........................................................................................................480

Hour 19, “Understanding Structures”..................................................................482

Quiz ................................................................................................................482

Exercises ........................................................................................................482

Hour 20, “Understanding Unions” ......................................................................486

Quiz ................................................................................................................486

Exercises ........................................................................................................486

Hour 21, “Reading and Writing with Files”........................................................490

Quiz ................................................................................................................490

Exercises ........................................................................................................490

Hour 22, “Using Special File Functions” ............................................................494

Quiz ................................................................................................................494

Exercises ........................................................................................................494

Hour 23, “Compiling Programs: The C Preprocessor” ......................................499

Quiz ................................................................................................................499

Exercises ........................................................................................................500

Index

503

00 067231861x FM 4.10.2000 10:53 AM Page xvii

About the Author

TONY ZHANG
has more than 15 years experience in computer programming and enter-prise-wide information system design. He is currently working for one of the “big 5”

consulting firms focusing on e-business related infrastructure design, development, and implementation.

With a Masters degree in Physics, he has published dozens of research papers on lasers and computer programming. Among his broad interests are oil painting and photography, the two things that Tony enjoys most.

You can reach Tony through Sams Publishing, or by emailing him at

[email protected].

About the Contributing Author

JOHN SOUTHMAYD
is a Software Design Engineer with experience in areas ranging from systems-level programming and device drivers to Windows development and Internet technologies. He currently works as a consultant with Excell Data Corporation and lives with his wife in Kirkland, Washington.

00 067231861x FM 4.10.2000 10:53 AM Page xviii

Dedication

To my wife, Ellen, and my parents, Zhi-ying and Bing-rong, for their love and inspirations.

—Tony Zhang

Acknowledgments

First, I’d like to thank the readers of the first edition of the book for their encouragement, patience, comments, and especially, criticisms, which made the second edition more suitable for people who want to start a journey in the C programming world.

It’s my great pleasure to work with editor Sharon Cox for the second time. I’d like to thank editors Carol Ackerman and Gus Miklos, and contributing author John Southmayd for their excellent work that made the second edition of the book more accessible, and largely, if not completely, error-free. Also, I’d like to express my appreciation to the great work of the other editing team members. Together, they made the second edition possible.

I greatly appreciate the love and support of my wife, Ellen, who inspires me to look at the technology world from different perspectives. It’s always a great joy to discuss issues on philosophy and literature with her. My parents, whom I can
never
thank enough, gave me not only love and affection, but also the opportunity of receiving the best education I could ever have when I was in China.

00 067231861x FM 4.10.2000 10:53 AM Page xix

Tell Us What You Think!

As the reader of this book,
you
are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way.

As an Associate Publisher for Sams, I welcome your comments. You can fax, email, or write me directly to let me know what you did or didn’t like about this book—as well as what we can do to make our books stronger.

Please note that I cannot help you with technical problems related to the topic of this
book, and that due to the high volume of mail I receive, I might not be able to reply to
every message.

When you write, please be sure to include this book’s title and author as well as your name and phone or fax number. I will carefully review your comments and share them with the author and editors who worked on the book.

Fax:

317-581-4770

Email:

[email protected]

Mail:

Michael Stephens

Associate Publisher

Sams Publishing

201 West 103rd Street

Indianapolis, IN 46290 USA

00 067231861x FM 4.10.2000 10:53 AM Page xx

01 067231861x intro 4.10.2000 10:58 AM Page 1

Introduction

If one learns from others but does not think, one will be bewildered;

If one thinks but does not learn from others, one will be in peril.

—Confucius

Welcome to the second edition of
Teach Yourself C in 24 Hours
!

Based on the success of the first edition of the book and the feedback from the readers, we have re-written or modified every single chapter of the book to make the second edition more suitable for beginners like you who want to get started with the C programming language as quickly as possible.

Of course, it’s very normal to spend more than 24 hours to really understand the concepts and programming skills introduced in the book. However, the good news is that this book offers many sample programs and exercises with clear explanations and answers, which makes the concepts of the C language easier to understand.

In fact,
Teach Yourself C in 24 Hours
provides a good starting point for you in C programming. It covers important topics in C programming, and lays a solid foundation for a serious beginner like you. After reading this book, you’ll be able to write basic C programs on your own.

You will profit from reading the book when you start to apply C programs to real problems or move on to learn other programming languages, such as Perl, C++, and Java.

Who Should Read This Book?

If this is your first time learning C, this book is written for you. In fact, in writing this book I assume that the readers have no previous programming experience. Of course, it’s always a big plus if you have some knowledge of computers.

Special Features of This Book

This book contains the following special elements that make it simpler and clearer for you to digest the rudimentary features and concepts of C as they are introduced:

• Syntax boxes

• Notes

• Cautions

• Tips

01 067231861x intro 4.10.2000 10:58 AM Page 2

2

Sams Teach Yourself C in 24 Hours

Syntax boxes
explain some of the more complicated features of C, such as control structures. Each syntax box consists of a formal definition of the feature followed by an explanation. Here is an example of a syntax box:

The syntax for the malloc() function is

AX

#include

void *malloc(size_t size);

YNTS
Here, size specifies the number of bytes of storage to allocate. The header file,

,

stdlib.h, has to be included before the malloc() function can be called. Because the malloc()

,

function returns a void pointer, its type is automatically converted to the type of pointer on the left side of an assignment operator.

(You’ll learn more about the malloc() function later in the book.)

Notes
are explanations of interesting properties of a particular C program feature. Let’s have a look at the following example of a note:

In left-justified output, the value being displayed appears at the left end of the value field. In right-justified output, the value being displayed appears at the right end of the value field.

Warnings
warn you of programming pitfalls you should avoid. Here is a typical warning: Never use the reserved keywords in C, nor names of the C library functions as variable names in your program.

Tips
are hints on how to write your C programs better. The following is an example of a tip:

If you have a complex programming project, break it into smaller pieces.

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

Other books

Lawyer Trap by R. J. Jagger
The relentless revolution: a history of capitalism by Joyce Appleby, Joyce Oldham Appleby
Love Is Blind by Lynsay Sands
Zack by William Bell
Uhura's Song by Janet Kagan