Sams Teach Yourself C in 24 Hours (9 page)

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

03 067231861x CH01 4.10.2000 10:59 AM Page 13

Taking the First Step

13

The C language’s readability and maintainability benefit directly from its relative close-ness to human languages, especially English.

1

Each high-level language needs a
compiler
or an
interpreter
to translate instructions written in the high-level programming language into a machine language that a computer can understand and execute. Different machines may need different compilers or interpreters for the same programming language. For instance, I use Microsoft’s C compiler to compile the C programs in this book for my personal computer (PC). If I need to run the C programs on a UNIX-based workstation, I have to use another type of C compiler to compile these programs. Therefore, the portability of programs written in C is realized by re-compiling the programs with different compilers for different machines (see Figure 1.2).

FIGURE 1.2

Porting programs writ-

The C

Program

ten in C into different

types of computers.

Compiler

Compiler

Compiler

A

B

C

The Computer’s Brain

You may know that the brain of a computer is the central processing unit (CPU). Some computers may have more than one CPU inside. A CPU has millions of transistors that make use of electronic switches. The electronic switches have only two states: off and on.

(Symbolically, 0 and 1 are used to represent the two states.) Therefore, a computer can only understand instructions consisting of series of 0s and 1s. In other words, machine-readable instructions have to be in binary format.

03 067231861x CH01 4.10.2000 10:59 AM Page 14

14

Hour 1

However, a computer program written in a high-level language, such as C, Java, or Perl, is just a text file, consisting of English-like characters and words. You have to use special programs, called compilers or interpreters, to translate such a program into a machine-readable code. That is, the text format of all instructions written in a high-level language has to be converted into binary format. The code obtained after the translation is called
binary code
. Prior to the translation, a program in text format is called
source code
.

The smallest unit of binary code is called a
bit
(from binary digit), which can have a value of 0 or 1. Generally, eight bits make up one
byte
, and half a byte (four bits) is one
nibble
.

In addition, the C language has other advantages. Programs written in C can be reused.

You can save parts of your C programs into a library file and invoke them in your next programming project simply by including the library file. Many common and useful programming tasks are already implemented in libraries that come included with compilers.

In addition, libraries allow you to easily unleash the power and functionality of the operating system you are using. More details on using C library functions are covered in the rest of this book.

C is a relatively small programming language, which makes life easier for you. You don’t have to remember many C keywords or commands before you start to write programs in C to solve problems in the real world.

For those who seek speed while still keeping the convenience and elegance of a high-level language, the C language is probably the best choice. In fact, C allows you to get control of computer hardware and peripherals. That’s why the C language is sometimes called the lowest high-level programming language.

Many other high-level languages have been developed based on C. For instance, Perl is a popular programming language in World Wide Web (WWW) design across the Internet.

Perl actually borrows a lot of features from C. If you understand C, learning Perl is a snap. Another example is the C++ language, which is simply an expanded version of C, although C++ makes object-oriented programming easier. Also, learning Java becomes much easier if you already know C.

There are generally two types of programming languages:
compiled
languages and
interpreted
languages.

A compiler is needed to translate a program written in a compiled language into machine-understandable code (that is, binary code) before you can run the program on your machine. When the translation is done, the binary

03 067231861x CH01 4.10.2000 10:59 AM Page 15

Taking the First Step

15

code can be saved into an application file. You can keep running the appli-1

cation file without the compiler unless the program (source code) is updated and you have to recompile it. The binary code or application file is also called executable code (or an executable file).

On the other hand, a program written in an interpreted language can be

run immediately after you finish writing it — or for that matter, while you are writing it! But such a program always needs an interpreter to translate the high-level instructions into machine-understandable instructions (binary code) at runtime. You cannot run the program on a machine unless the right interpreter is available.

You can think of the C language as a compiled language because most C

language vendors make only C compilers, as opposed to interpreters, to support programs written in C.

However, there is nothing inherent to a compiled language to prevent

someone from providing an interpreter for the language; likewise, people can and often do write compilers for interpreted languages. In fact, it is not uncommon to mix the two flavors of languages, where a programmer compiles source code into a small binary file which is then executed by a runtime interpreter.

The ANSI C Standard

For many years, the de facto standard for the C programming language was the book
The
C Programming Language
, written by Brian Kernighan and Dennis Ritchie in 1978. This book is commonly known in the programming community as simply K&R (referring to the initials of the authors) and finds a place on many programmers’ bookshelves to this day. However, the book was written as a tutorial introduction to C, not as a comprehen-sive or official standard for the language. As different vendors offered varying implementations of the C language, differences between those implementations began to appear.

Fearing that C might lose its portability, a group of compiler vendors and software devel-opers petitioned the American National Standards Institute (ANSI) to build a standard for the C language in 1983. ANSI approved the application and formed the X3J11 Technical Committee to work on the C standard. By the end of 1989, the committee approved the ANSI standard for the C programming language.

The ANSI standard for C enhances the original K&R standard and defines a group of commonly used C functions that known as the ANSI C standard library. In most cases, C

compilers include the standard library, along with other libraries to provide some other compiler-specific functions.

03 067231861x CH01 4.10.2000 10:59 AM Page 16

16

Hour 1

This book focuses on the C functions defined in the ANSI standard, which is supported by all compiler vendors. All programs in this book can be compiled by any compilers that comply with the ANSI standard. If you’re interested in a specific compiler, you can learn the compiler-specific functions from the compiler’s reference manual.

Assumptions About You

No previous programming experience is required for you to learn the C language from this book, although some knowledge of computers helps. Also, it’s up to you to determine how quickly to go through the 24 hours of this book: You could sit up with a big pot of coffee and power through the book in a sitting or you could take an hour a day for 24 days.

After you complete this book, having done all of the exercises along the way, you should be proficient and comfortable with the syntax and features of the C language. In addition, you’ll already have some experience with many of the tasks that are encountered in C

programming. When you’re ready to undertake your own programming projects, you’ll be able to use C as a tool in writing the powerful and useful programs you want to create. As you progress, you’ll find that there is always more to learn—not only about C

and how to leverage its power, but also about new technologies and programming ideas in general. With hard work and lots of practice, you can quickly build on the skills and technologies that you learn.

Setting Up Your System

Basically, all you need is a computer and a C compiler in order to compile and run your own C programs or the C programs from this book. The recommended hardware and software are listed in the following sections.

Hardware

Any type of computer that has or can access a C compiler is fine. The C compiler should be ANSI C compliant. Most likely, you have a PC on your desktop. A 286 PC with a 50MB hard drive and 1MB memory (RAM) is probably the minimum requirement to run a DOS-based C compiler. For a Windows-based C compiler, your computer must have a bigger hard drive and more memory. Check your compiler vendor for more details on hardware requirements.

Software

If you’re using a UNIX-based workstation, you might already have a C compiler loaded on your machine, or at least you might be able to access a C compiler on a server machine. Check with your system administrator to find out how to access an ANSI C

03 067231861x CH01 4.10.2000 10:59 AM Page 17

Taking the First Step

17

compliant compiler. On a UNIX-based machine, you should know how to use a text editor such as vi or emacs to write C programs.

1

If you have a PC running a Microsoft Windows operating system (such as Windows 95), you need to install a C compiler and a text editor on your PC. However, most C compilers come with a built-in editor. You can also use any text editor that may already be installed on your machine.

Borland International’s Turbo C and Microsoft’s Quick C used to be very popular in the C compiler market. These days, an ANSI-compliant C compiler is usually part of any commercially available C++ development package, such as Microsoft Visual C++. In addition, development packages come with an integrated development environment (IDE), which you can use to edit, compile, run, and debug your C programs all from the same window.

You can pick up any C compiler you like to compile the sample code given in the book, as long as the compiler is ANSI C compliant. You shouldn’t have problems installing a C

compiler on your computer if you read the manuals that come with the compiler and follow the installation instructions correctly. Most C and/or C++ compilers provide a quick tutorial that shows you how to install the compiler and set up a working development environment on your computer.

These days, the Linux operating system is becoming more and more popular among PC

users. In most cases, the Linux package you get contains a C compiler. The C compiler can be installed on your PC when you’re installing the Linux operating system, or can be added later after you finish the installation of Linux.

A Sample C Programming Setup

I have a Pentium 100MHz PC with 32MB memory and with a 2.5GB hard drive. (The hard drive had about 1.5GB free space before I installed a copy of Microsoft Visual C++

5.0.) Also, I have Windows 95 as the operating system on the machine.

In this book, all C programs are developed with Microsoft Visual C++ version 5.0. The reasons I chose Visual C++ are simple: All C programs in this book are written in ANSI C and can be compiled into console-mode applications (that is, text-based programs running in a DOS window); the Visual C++ 5.0 package includes a good C compiler that is ANSI C compliant.

I set up my development environment in such a way that all C programs in this book can be compiled and made into console applications. Also, I test and run the applications made from the C programs at a DOS prompt provided by Windows 95.

In the following two sections, I’ll briefly show you how to use Microsoft’s and Boralnd’s C compilers.

03 067231861x CH01 4.10.2000 10:59 AM Page 18

18

Hour 1

Using Microsoft’s Compiler

I’m going to show you how to use the C compiler that is bundled with the Microsoft’s Visual C++ package in this section. If you need to learn more details on how to install Visual C++, please follow the instructions that come with the compiler.

Now I assume you’ve installed a copy of Visual C++ 5.0 on your computer. To start the compiler, you can click the Start button from your Windows 95 (or 98 or NT), and choose: Programs, Microsoft Visual C++ 5.0, Microsoft Visual C++ 5.0. Or, you can simply run the application file MSDEV.EXE directly from the directory (folder) where you installed the Visual C++ package. Figure 1.3 shows an example of the integrated development environment (IDE) from Visual C++ 5.0.

FIGURE 1.3

An example of the IDE

Other books

Death Sentences by Kawamata Chiaki
Dominate Me by Jambrea Jo Jones
Hunted by Christine Kersey
Righteous Obsession by Riker, Rose
The Wet Nurse's Tale by Erica Eisdorfer
Demonic Attraction by Kim Knox
Invasion: Colorado by Vaughn Heppner
Second Kiss by Palmer, Natalie