Sams Teach Yourself C in 24 Hours (12 page)

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

The preprocessor is a program that does some preparations for the C compiler before your code is compiled. More details about the C preprocessor are discussed in Hour 23,

“Compiling Programs: The C Preprocessor.”

Also in this line, you see that follows #include. You may guess that the file the #include directive asks for is something called stdio.h. You are exactly right! Here, the #include directive does ask the C preprocessor to look for and place stdio.h at the location of the directive in the C program.

04 067231861x CH02 1/25/00 10:42 AM Page 32

32

Hour 2

The name of the stdio.h file stands for
standard input-output header
file. The stdio.h file contains numerous prototypes and macros to perform input or output (I/O) for C programs. You’ll see more program I/O in Hour 5, “Handling Standard Input and Output.”

Some operating systems distinguish between upper- and lowercase letters, while others do not. For instance, stdio.h and STDIO.H are identical filenames on a PC, while they are different in UNIX.

Header Files

The files that are included by the #include directive, like stdio.h, are called
header
files
because the #include directives are almost always placed at the start, or head of C

programs. Actually, the extension name of .h does mean “header” and these are sometimes referred to in conversation as dot-h files.

Besides stdio.h, there are more header files, such as stdlib.h, string.h, math.h, and so on. Appendix A, “ANSI Standard Header Files,” gives a list of all the ANSI standard header files. The specific header files you need to include depend on the specific library functions you intend to call. The documentation for library functions will tell you which header file is required.

Angle Brackets (
< >
) and Double Quotes (
“ “
)

In the second line of Listing 2.1, there are two angle brackets, < and >, that are used to surround stdio.h. You may be wondering what the angle brackets do. In C, the angle brackets ask the C preprocessor to look for a header file in a directory other than the current one.

For instance, the current directory containing the 02L01.C file is called C:\code on my computer. Therefore, the angle brackets around tell the C preprocessor to look for stdio.h in a directory other than C:\code.

If you want to let the C preprocessor look into the current directory first for a header file before it starts to look elsewhere, you can use double quotes to surround the name of the header file. For instance, when the C preprocessor sees “stdio.h”, it looks in the current directory, which is C:\code on my machine, first before it looks elsewhere for the stdio.h header file.

Normally, the header files are saved in a subdirectory called include. For instance, I install a Microsoft C compiler in the directory MSVC on my hard drive, which is labeled as the C drive. Then the path to access the header files becomes C:\MSVC\include.

04 067231861x CH02 1/25/00 10:42 AM Page 33

Writing Your First C Program

33

The path where header files are kept is usually determined by your compiler when you install it. This is commonly referred to as the include directory or include path of your environment. Normally, you never need to worry about the include directory until you create your own header files. For now, you just need to specify the name of the header file you wish to include.

The
main()
Function

2

In line 4 of Listing 2.1, you see this function:

main ()

This is a very special function in C. Every C program must have a main() function, and every C program can only have one main() function. More generic discussions about functions are given in Hour 3, “Learning the Structure of a C Program.”

You can put the main() function wherever you want in your C program. However, the execution of your program always starts with the main() function. If you create other functions in your program, main() will always execute first, even if it is at the bottom of your program file.

In Listing 2.1, the main() function body starts in line 4 and ends in line 8. Because this is a very simple program, the main() function is the only function defined in the program.

Within the main() function body, a C library function, printf(), is called in order to print out a greeting message (see line 6). More details about printf() are covered in Hour 5.

One more important thing about main() is that the execution of every C program ends with main(). A program ends when all the statements within the main() function have been executed.

The Newline Character (
\n
)

In the printf() function, one thing worth mentioning at this point is the
newline
character, \n. Usually suffixed at the end of a message, the newline character tells the computer to move the cursor to the beginning of the next line so that anything printed out after the message will start on the next line on the screen.

In a UNIX environment, \n by itself goes to a new line but leaves the cursor at the position it was on the previous line. In this case, it is necessary to print \r\n rather than just \n. The

\r character is the
carriage return
character. When you run the sample programs in this book, you will be able to tell right away whether the cursor is returning to the beginning of the new line; if it is not, simply use \r\n wherever you see \n in the program listings.

Exercise 3 in this lesson gives you a chance to use the newline character to break a one-line message into two lines.

04 067231861x CH02 1/25/00 10:42 AM Page 34

34

Hour 2

The
return
Statement

All functions in C can return values. For instance, when you create a function to add two numbers, you can make such a function that returns to you the value of the addition.

The main() function itself returns an integer value. In C, integers are decimal numbers without fraction portions.

Therefore, in line 7 of Listing 2.1, there is a statement, return 0;, that indicates that 0 is returned from the main() function and the program is terminated normally. There are cases when you must end your program due to an error condition. When that happens, you can return values other than 0 to tell the operating system (or the program that ran your program) that there was an error.

The
exit()
Function

There is also a C library function, exit(), that can be used to end a program. Because the exit() function is defined in a header file, stdlib.h, you have to include the header file at the beginning of your program in order to use the function. The exit() function itself does not return a value to your program.

Note that return and exit() can also be used in other functions. You’ll see more examples of the return keyword in the rest of the book.

Compiling and Linking

You may already be anxious to know how an executable file is made. Let’s have a look how a C program is compiled and translated into an executable file. As shown in Figure 2.1, there are at least three steps needed to create an executable file.

First, a program file written in C, called
source code
, is made. The name of the source code file ends with the extension .c.

Then the source code file is compiled by a C
compiler
, which creates a new file. The new file is an
object file
. In UNIX operating system, the name of an object file ends with the extension .o; In the DOS and Windows operating systems, the extension is .obj.

You cannot execute the object file because there is some function code missing. You have to finish the next step: linking. Linking is done by invoking a special program called a
linker
, which normally comes with the compiler package.

A linker is used to link together the object file, the ANSI standard C library, and other user-generated libraries to produce an executable file—the binary code. In this stage, the binary code of the library functions that are called in the source code is combined

04 067231861x CH02 1/25/00 10:42 AM Page 35

Writing Your First C Program

35

with the object file; the result is saved into a new file—an executable file. As you learned in the first chapter of the book, the name of an executable file usually ends with the extension .exe in DOS and Windows. (.com is another extension used for an DOS

executable filename.) In UNIX, it’s not necessary to include such an extension to an executable filename.

FIGURE 2.1

The header files

The C program

Make an executable

stdio.h

2

file by the compiler

stdlib.h

/* 02L01.C */

and linker.

.....

Library files

The C Compiler

&

Other object files

The Linker

The executable file

10011111001

0111000011

110001100

Later, you’ll learn that in many cases, there may be several object files that have to be linked together in order to make an executable program.

Note that the object file and executable file are both machine-dependent. You cannot simply move an executable file from the current computer platform to another one that is operated by a different operating system, although the source code of the executable file, presumably written in ANSI C, might be machine independent (that is, portable).

04 067231861x CH02 1/25/00 10:42 AM Page 36

36

Hour 2

Portability is an important concept in C, as it was one of the original design goals of the language. The portability of C code is what fueled its wide-spread use and popularity. Back in the days when applications and operating systems were hand-tailored to a specific computer system, software had to be written from scratch every time a new computer came along. The advent of the C language abstracted software away from hardware, and in a very real sense, helped give rise to the software industry as we know it today.

Portability, in essence, refers to the process of porting a program to a different computer and/or operating system. Software development is an expen-

sive and time-consuming process, and rewriting an existing program to work on a new computer is often a daunting task that is best avoided. Assuming that each operating system and compiler combination is tailored to the specific computer where it was intended to run, a C program should be easily portable with only minimal changes to the code. Portability is mentioned throughout this book, as it is important that C programs not make assumptions about the specific computer system where they are running.

Fortunately, the ANSI C standard provides many features and functions that allow your program to essentially adapt to its environment, as opposed to acting on assumptions made by you, the programmer. If you get into the

habit of maintaining portability early on, then as you progress with C, the idea will become second nature.

What’s Wrong with My Program?

Other books

Out in the Open by Jesús Carrasco
The Broken Window by Jeffery Deaver
The Cost of Love by Parke, Nerika
Beautiful Beloved by Christina Lauren
Other Women by Fiona McDonald
Teresa Medeiros by Thief of Hearts
A Charge of Valor by Morgan Rice
Texas Hold Him by Lisa Cooke