Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Introduction

Welcome to C For Dummies, 2nd Edition — your last, desperate, and final attempt to understand the C programming language.

Although I can’t promise that you’ll become a C guru after wading through this text, I can guarantee that you will

Know how to recognize a C program and, when one is grouped with an IRS Form 1040, the morning stock report, baseball statistics, and anything written in Braille, you’ll be able to pick out which one is the C program.

Be able to write C programs that no other publisher would let an author print in its C books.

Appreciate the following code, but be unable to use it at cocktail parties to impress your friends:

while(dead_horse)

beat();

Find out how to speak in C Talk, which is the ability to look at character groupings, such as printf, putchar, and clock, and pronounce them as “print-f,” “put-kar,” and “see-lock.”

Have fun.

I can’t really guarantee that last point. However, this book was written minus the sword of mathematics hanging over anyone’s head. Let’s leave stern program­ ming up to those who fuss over Avogadro’s number and Fibonacci sequences and who debate the merits of how to indent their C program source code. Serious work is for the nerds. Fun happens when you read C For Dummies, 2nd Edition.

“What Will Understanding C Do for Me?”

Look at your computer screen. Imagine something happening there. Anything. As long as you know how to program a computer, what you imagine will take place. Okay, maybe not as fast as you like — but it can be done.

Programming is the ultimate way to get even with a computer. You are in charge. You tell the beast what to do. And it will obey you, even when you tell it to do something stupid. Computers are fast and obedient, not smart.

2C For Dummies, 2nd Edition

Anything your computer does, any devices it talks with or controls, can be manipulated by using a programming language and writing programs that pull the right levers. The C programming language has been crowned the best and most common way to program any personal computer. C may not be the easi­ est programming language to figure out, but it’s not the most difficult, either. It’s tremendously popular and well supported, which makes it a good choice.

About This Here Dummies Approach

Most programming books start out by assuming that you don’t know anything. The author may remember that for, oh, maybe two or three chapters. Then, after that initial pressure is off, there he goes! Chapter 4 is written not to teach you how to program, but, rather, to impress the author’s programming buddies back at college. So your learning journey ends with a whimper. You will not find that problem in this book.

The best way to learn something is one piece at a time. With programming, I prefer to show you things by using small programs, tiny models, and quick- to-type examples. That way, you’re not overwhelmed with an initial program that’s three pages long, and you don’t get lost after a few chapters. That’s because the pace stays the same throughout the book. I insist on it!

This book also gets you started right away. When researching other books, I noticed that often the first program you have to type is not only several dozen lines long, but also nearly 50 pages into the book on average! In this book, you get started right away with a program example on Page 13. That quick!

How to Work the Examples in This Book

Part of the fun of finding out how to program by reading a book is that you type the programs yourself. That’s the way I figured out how to program a com­ puter. I sat down with Dr. David Lien’s Learning TRS-80 BASIC (Compusoft) and, 36 solid hours later, I finished. Then I slept. Then I did it again because I com­ pletely forgot everything, but remembered enjoying doing it the first time.

Your first task is to read Appendix A. It tells you how to set up a C language compiler on your computer and get things all ready to work.

Next, you need to know how to type stuff. This stuff looks like this:

Here I go, typing some stuff. La, la, la.

Introduction 3

Mostly, you type complete programs, consisting of several lines like the one before this paragraph. Type them all, and press Enter at the end of each line. Because this book is only so wide, however, occasionally you see a line split in two. It looks like this:

This is an example of a very long line that was painfully

split in two by this book’s cruel typesetters.

When you see that, don’t type two lines. If you just keep typing, everything fits on one line on your screen. If you forget this advice, your programs mess up, so I toss in several reminders throughout this book whenever such a thing happens.

Foolish Assumptions

This book makes the following assumptions about you, your computer, your compiler, and — most important — your state of mind:

You have a computer, or at least you have access to one. It can be just about any computer; this book is not specific to Windows.

You’re pretty good with the computer. You understand things. You may even fix your own problems or help others with their problems.

You know how to look things up on the Web, download stuff, and find things you need.

You have a passing familiarity with your operating system’s command prompt or terminal window. This is important, and it’s explained in Appendix A.

You’re willing to find out how to program — perhaps even desperate to do so!

Icons Used in This Book

Technical information you can merrily skip over.

Something you should remember to do.

4C For Dummies, 2nd Edition

Something you should remember not to do.

A healthy suggestion worthy of note.

What’s New with This Edition?

This book isn’t really the second edition of any previous book, but it does borrow material from the old C For Dummies books, Volumes I and II. This book represents a compilation of basic material from both books. And, by reading this book, you’ll have a broad, basic knowledge of the C language.

Unlike the older books, this one is organized on a chapter-by-chapter level, not by lessons. Each chapter is self-contained and, where necessary, cross references to other chapters are included.

Gone are the quizzes and tests. This book has no homework, per se.

Alas, this book is only so big, and only so much ground could be covered, given this book’s gentle pace. Because of that, those interested in pursuing the C language further should check out the companion book, C All-in-One Desk Reference For Dummies (Wiley). That book is for more experienced program­ mers, which is what you will become after reading this book.

Final Thots

Understanding how to use C is an ongoing process. Only a dweeb would say “I know everything about programming in C.” There are new things to be learned every day and different approaches to the same problems. Nothing is perfect, but many things are close.

My thoughts on the matter are this: Sure, people who took 20 years of C pro­ gramming and paid too much per semester at A Major University will have some C snobbishness in them. Whatever. Ask yourself this question: Does my program run? Okay. Does it do what I want? Better. Does it meet their artifi­ cial standards? Who cares? I’ll be happy if your sloppy C program works. But keep this in mind: The more you learn, the better you get. You’ll discover new tricks and adapt your programming style to them.

Introduction 5

This book has a companion Web page, replete with bonus material and all sorts of fun information:

http://www.c-for-dummies.com

I hope that you enjoy the journey you’re about to begin. Crack your knuckles, power up that compiler, and prepare yourself for a few solid hours of eyeball frazzle. You’re going C programming!

6C For Dummies, 2nd Edition

Part I

Introduction to C Programming

In this part . . .

You have never programmed anything in your life. The VCR? Forget it! On your microwave oven, you use the

Popcorn and Add a Minute buttons. You know that you can punch numbers into your cell phone and hit the Send button, yet you dare not touch any of the other buttons, for fear of entering that dark realm, that dank and musty dungeon of programming. If that’s you, get ready to turn your life around.

Contrary to what you may believe, it’s nothing to program a computer. Anyone can do it. Programmers may carry themselves with an air of mysticism and treat their skills like priests performing sacred religious rites. Poppycock. Programming is painless. It’s easy. It’s fun.

It’s now your turn to tell the computer exactly what to do with itself. In just a few pages, you will be programming your PC. It’s time to get even! Time to twist its arm and wait until it bellows “Uncle! UNCLE!” Get ready to take charge.

Chapter 1

Up from the Primordial C

In This Chapter

Hysterical C history

How C programs are created

Building the source code

Compiling and linking

Running the result

As the most useful device you have ever used, a computer can become anything — as long as you have the ability to program it. That’s what makes computers unique in the pantheon of modern devices. And although

most computer users shy away from programming — confusing it with math­ ematics or electrical engineering — the fact is that programming a computer is really a rather simple and straightforward thing. It’s easy.

This chapter introduces you to the basics of programming. Although it has some yabber-yabber and background information, the meat of the chapter involves creating, compiling, and running your first program. Feel the power! Finally, it’s you who can tell the computer what to do with itself!

Because you probably didn’t read this book’s Introduction (for shame), know that you should preview Appendix A before starting here.

An Extremely Short and Cheap

History of the C Language

First, there was the B programming language. Then there was the C program­ ming language.

10

Part I: Introduction to C Programming

Stuff you don’t need to know about language levels

Programming languages have different levels, depending on how much they resemble human languages. Programming languages that use common words and are relatively easy for most folks to read and study are called highlevel languages. The opposite of those are low-level languages, which are not easy to read or study.

High-level languages include the popular BASIC programming language as well as other lan­ guages that just aren’t that popular any more. BASIC reads almost like English, and all its com­ mands and instructions are English words — or at least English words missing a few vowels or severely disobeying the laws of spelling.

The lowest of the low-level programming lan­ guages is machine language. That language is the actual primitive grunts and groans of the microprocessor itself. Machine language con­ sists of numbers and codes that the micro­ processor understands and executes. Therefore, no one really writes programs in machine lan­ guage; rather, they use assembly language, which is one step above the low-level machine

language because the grunts and groans are spelled out rather than entered as raw numbers.

Why would anyone use a low-level language when high-level languages exist? Speed! Pro­ grams written in low-level languages run as fast as the computer can run them, often many times faster than their high-level counterparts. Plus, the size of the program is smaller. A program written in Visual Basic may be 34K in size, but the same program written in assembly language may be 896 bytes long. On the other hand, the time it takes to develop an assembly language program is much longer than it would take to write the same program in a higher-level lan­ guage. It’s a trade-off.

The C programming language is considered a mid-level language. It has parts that are lowlevel grunting and squawking, and also many high-level parts that read like any sentence in a Michael Crichton novel, but with more charac­ ter development. In C, you get the best of the high-level programming languages and the speed of development they offer, and you also get the compact program size and speed of a low-level language. That’s why C is so bitchen.

No, I’m not being flip. C was developed at AT&T Bell Labs in the early 1970s. At the time, Bell Labs had a programming language named B — B for Bell. The next language they created was C — one up on B.

C is the offspring of both the B programming language and a language named BCPL, which stood for Basic Combined Programming Language. But you have to admit that the B story is cute enough by itself.

You would think that the next, better version of C would be called the D language. But, no; it’s named C++, for reasons that become apparent in Chapter 16.

C is considered a mid-level language. See the nearby sidebar, “Stuff you don’t need to know about language levels,” for the boring details.