-->

Previous | Table of Contents | Next

Page 458

wider program context that calls it; it simply transforms the values found within the input variables (parameters), whatever they might be, and returns the result to whatever other function invokes it.


Figure 23.1.
Nesting function calls
within C programs.


Because procedures written in C are implemented as functions, they don't need to know whether (or how deeply) they will be nested inside other function calls. This enables you to reuse C functions in many different programs without modifying them. For example, Function 2 in Figure 23.1 might be called directly by the Main logic in a different C program.

An entire C program is itself a function that returns a result code, when executed, to the program that invokes it. This is usually a shell in the case of applications, but might also be any other part of the operating system or any other UNIX program. Because C programs are all structured as functions, they can be invoked by other programs or nested inside larger programs without needing to be rewritten in any way.

NOTE
C's feature of structuring programs as functions has heavily shaped the look and feel of UNIX. More than in most other operating environments, UNIX systems consist of many small C programs that call one another, are combined into larger programs, and are invoked by the user as needed. Instead of using monolithic, integrated applications, UNIX typically hosts many small, flexible programs. Users can customize their working environments by combining these tools to do new tasks.

Data in C Programs

The two kinds of data that are manipulated within C programs are literal values and variables. Literal values are specific, actual numbers or characters, such as 1, 4.35, or a. Variables are names associated with a place in memory that can hold data values. Each variable in C is typed; that

Page 459

is, each variable can hold only one kind of value. The basic datatypes include integers, floating-point (real) numbers, characters, and arrays. An array is a series of data elements of the same type; the elements are identified by the order in which they appear (by their place within the series).

You can define complex data structures as well. Complex data structures are used to gather a number of related data items together under one name. A terminal communications program, for example, might have a terminal control block (TCB) associated with each user who is logged on. The TCB typically contains data elements identifying the communications port, the active application process, and other information associated with that terminal session.

All the variables in a C program must be explicitly defined before they can be used.

Creating, Compiling, and Executing Your First Program

The development of a C program is an iterative procedure. Many UNIX tools familiar to software developers are involved in this four-step process:

  1. Using an editor, write your code into a text file.
  2. Compile the program.
  3. Execute the program.
  4. Debug the program.

Repeat the first two steps until the program compiles successfully. Then begin the execution and debugging. When I explain each of these steps in this chapter, you might find that some of the concepts seem strange, especially if you're a nonprogrammer. Remember that this chapter serves as only an introduction to C as a programming language. For more in-depth coverage of C, check out one of the resources listed at the end of this chapter in the section "Additional Resources."

The typical first C program is almost a cliché—the Hello, World program, which prints the simple line Hello, World. Listing 23.1 contains the source code of the program.

Listing 23.1. Source code of the Hello, World program.


main()

{

printf("Hello, World\n");

}

This program can be compiled and executed as follows:


$ gcc hello.c

$ ./a.out

Hello, World

$

Page 460

NOTE
If the current directory is in your path, you could have executed a.out by typing a.out

The Hello, World program is compiled with the gcc command, which creates a file a.out if the code is correct. Just typing a.out will run it. Notice that Listing 23.1 includes only one function, main. Every C program must have a main function; main is where the program's execution begins. The only statement in Listing 23.1 is a call to the printf library function, which passes the string Hello, World\n. (Functions are described in detail later in this chapter in the section "Functions.") The last two characters of the string, \n, represent the newline character.

NOTE
a.out is the default filename for executables (binaries) created by the C compiler under UNIX. This can be changed through the use of a command-line switch (see the section "GNU C/C++ Compiler Command-Line Switches" later in this chapter).

An Overview of the C Language

As with all programming languages, C programs must follow certain rules. These rules include how a program should appear and what the words and symbols mean; altogether, these are called the syntax of a programming language. Think of a program as a story. Within a story, each sentence must have a noun and a verb, put together with a particular syntax. Sentences form paragraphs, and the paragraphs tell the story. Similarly, C statements constructed with the correct syntax can form functions and programs.

Elementary C Syntax

Like all languages, C deals primarily with the manipulation and presentation of data. The language C evolved from, BCPL, dealt with data only as data. C, however, goes one step further by using the concept of datatypes. The three basic datatypes are integers, floating-point numbers, and characters. Other datatypes are built from these.

Integers are the basic mathematical datatype. They can be classified as long and short integers, and their size is implementation dependent. With a few exceptions, integers are four bytes in length, and they can range from -2,147,483,648 to 2,147,483,647. In ANSI C, these values are defined in a header (limit.h) as INT_MIN and INT_MAX. The qualifier unsigned moves the range one bit higher, to the equivalent of INT_MAX minus INT_MIN.

Floating-point numbers are used for more complicated mathematics, whereas integer mathematics is limited to integer results. For example, with integers, 3/2 equals 1. Floating-point

Previous | Table of Contents | Next