1. What Is C Programming?

Rewarding and Fun

image

Although some people consider C to be difficult to learn and use, you’ll soon see that they are wrong. C is touted as being a cryptic programming language, and it can be; but a well-written C program is just as easy to follow as a program written in any other programming language. The demand for C programmers today is high, and there is no end in sight to that demand.

If you’ve never written a program in your life, this chapter begins at the beginning, teaching you introductory programming concepts, explaining what a program is, and providing a short history of the C language. Get ready to be excited! C is a programming language rich in its capabilities.

What Is a Program?

A computer isn’t smart. On your worst days, you are light-years ahead of your computer in intelligence. The computer’s only advantage is that it obeys your instructions. Your computer will sit for days, processing the data you supply, without getting bored and without wanting overtime pay.

The computer can’t decide what to do on its own. Computers can’t think for themselves so programmers (people who tell computers what to do) must give computers extremely detailed instructions. Without instructions, a computer is useless. A computer can no more process your payroll without detailed instructions than an automobile can start by itself and drive around the block by itself. The collection of detailed instructions that you supply when you want your computer to perform a specific task is known as a program.

Note

image

Word processors, computer payroll systems, computer games, and electronic spreadsheets are nothing more than computer programs. Without such programs, the computer would just sit there, not knowing what to do next. A word processing program contains a list of detailed instructions, written in a computer language such as C, that tells the computer exactly how to be a word processor. When you program, you are telling the computer to follow the instructions in the program you have supplied.

There are thousands of programs you can buy for your computer, but when a business wants a computer to perform a specific task, that business hires programmers to write programs that follow the specifications needed by the business. You can make your computer do many things, but you might not be able to find a program that does exactly what you want. This book rescues you from that dilemma. After you learn C, you will be able to write programs that contain instructions that tell the computer how to behave.

Clue

image

A computer program tells your computer how to do what you want. Just as a chef needs a recipe to make a dish, a program needs instructions to produce results. (See Figure 1.1.) A recipe is nothing more than a set of detailed instructions that, if properly written, describes the proper sequence and contents of the steps needed to prepare a certain dish. That’s exactly what a computer program is to your computer.

Figure 1.1. Just as a chef needs a recipe to cook, your computer needs a program to know what to do next.

image

Programs produce output when you run or execute them. The prepared dish is a recipe’s output, and the payroll or word processor is the output produced by a running program.

What You Need to Write C Programs

Before you can write and execute a C program on your computer, you need a C compiler. A C compiler takes the C program you write and compiles it (which is a technical term for making the program computer-readable), enabling you to run the compiled program when you’re ready to look at the results. Today’s C compilers are much more advanced than the language compilers of a few years ago. They offer full-screen editing, pull-down menus, and online help to provide more assistance to the beginning programmer.

It is hard to find a C compiler these days. Most of the time, C compilers come bundled (techie-speak for included with) an advanced version of C, known as C++. Therefore, when you shop for a C compiler, you will almost always find a C and a C++ compiler combined in the same box. Buy the combined C++/C package, you will have C now and C++ when you’re ready to learn it.

Note

image

The last chapter of this book briefly describes how C++ differs from and improves upon C.

The most popular C compilers today are Turbo C++ and Borland C++, both made by Borland International, Inc. Borland C++ is basically Turbo C++ along with many additional programs that help advanced C and C++ programmers.

Microsoft Corporation offers a powerful version of C++ and C called Visual C++. As with Borland C++, Visual C++ provides you with advanced DOS, Windows, and Windows NT programming. You can write Windows programs with Turbo C++ as well, but Turbo C++ does not include as many add-on Windows programming tools as Borland C++ and Visual C++.

By the way, it is very difficult to write Windows programs, especially for people who are new to programming. Don’t be in a hurry to bite off more than you can chew. First learn to write simple DOS-based C programs (and this book is all you need for that!) and then you can gradually migrate over to the more difficult areas of C++ and Windows programming.

There are other C compiler vendors on the market, but Borland and Microsoft lead the pack in sheer numbers of C programming customers.

Warning

image

The C program you write is called source code. A compiler takes C source code and translates that code into machine language. Computers are made up of nothing more than thousands of electrical switches that are either on or off. Therefore, computers must ultimately be given instructions in binary. The prefix bi means two, and the two states of electricity are called binary states. It’s much easier to use a C compiler to convert your C programs into 1s and 0s that represent internal on and off switch settings than for you to do it yourself.

The Programming Process

Most people follow these basic steps when writing a program:

  1. Decide exactly what the program is to do.
  2. Use an editor to write and save your programming language instructions. An editor is a lot like a word processor (although not usually as fancy) that lets you create and edit text. All the popular C compilers include an integrated editor along with the programming language compiler. All C program filenames end in the .C file extension.
  3. Compile the program.
  4. Check for program errors. If there are any, fix them and go back to step 3.
  5. Execute the program.

Note

image

An error in a computer program is called a bug. Getting rid of errors is called debugging a program.

Today’s C compilers, such as Turbo C++, let you perform these five steps easily, all from within the same environment. For instance, if you have Turbo C++, you can use Turbo C++’s editor, compile your program, view any errors, fix the errors, run the program, and look at the results, all from within the same screen and using a uniform set of menus.

In a nutshell, most C compilers require only this of you when you write C programs: Start the compiler, type your program, and then select Run from the menu. Turbo C++ includes the shortcut keystroke Alt+R (press and hold the Alt key, press the R key, and then let up on both) followed by Enter to compile and run your program. The compiler takes care of compiling and executing the program and informing you of any errors.

Clue

image

Many times, your C compiler can find bugs in your programs. If you spell a command incorrectly, for instance, your C compiler informs you when you compile the program.

Just in case you still don’t fully understand the need for a compiler, your source code is like the raw materials that your computer needs. The compiler is like a machine that converts those raw materials to a final product, a compiled program, that the computer can understand.

Using C

C is one of the most popular programming languages in use today. Because of the many possible versions of C, a committee known as the ANSI committee developed a set of rules (known as ANSI C) for all versions of C. As long as you run programs using an ANSI C compiler, you can be assured that you can compile your C programs on almost any computer that has an ANSI C compiler. By choosing the appropriate setting, you can make most compilers, including Borland’s and Microsoft’s, ANSI C compatible. (See your compiler manual for details.)

Clue

image

As soon as you compile a C program, you can run the compiled program on any computer that is compatible with yours, whether or not the computer has an ANSI C compiler.

C is more efficient than most programming languages. It is also a relatively small programming language. In other words, you don’t have to learn many commands in C. Throughout this book, you will learn about C commands and other elements of the C language, such as operators, functions, and preprocessor directives.

Warning

image

Put on your thinking cap and set your phaser on C because the next chapter takes you on a journey through your first C program.

Rewards

image

• Get a C compiler and install it on your computer. Most compilers come with a quick tutorial that helps you load the compiler onto your computer’s hard disk.

• Learn the C programming language. This book takes care of that! As you learn more about C, try to stay with ANSI C commands instead of using compiler-specific C functions that might not be available in other compilers you use later.

Pitfalls

image

• Don’t be nervous, because C programs can be easy to write. C compilers often have many more features than you will ever have to learn.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.6.194