Previous Table of Contents Next


Chapter 3
C Programming

by Steve Summit

3.1. Introduction

C is an extremely popular, relatively low-level, procedural programming language. As the language has evolved, its definition has passed through several stages, representing distinct dialects of the language. As of 1998, the vast majority of C programming is performed in accordance with the ANSI/ISO C Standard, first ratified as ANSI X3.159 in 1989 and then as ISO/IEC 9989 in 1990. The description in this chapter is primarily of the C language as defined in that standard, with notes concerning a few aspects of pre-ANSI C (also known as “K&R” C, after Kernighan & Ritchie, 1988), and of some changes that can be anticipated in the next revision of the C Standard (known informally as “C9X” and expected to be finalized in the next few years). The C Standard was amended significantly in 1994 by Normative Addendum 1 (“NA1”), and those changes are reflected in this chapter.

The history and philosophy of C are covered in Chapter 2 of this volume, so we will not dwell further on those topics here. Without further ado, then, we will proceed immediately with two sample programs, to set the stage for a reasonably complete treatment of the language in later sections.

3.1.1. Sample Programs

The traditional first C program is one that prints a certain well-worn phrase to the standard output device:

#include <stdio.h>

int main()
{
       printf(“Hello, world!\n”);
       return 0;
}

This tiny example displays many of the features of any C program. The syntax int main() { … } defines a function named main with return type int. If the function accepted any parameters, they would be listed in the first pair of parentheses. The braces ({}) surround the body of the function, which in this case consists of just two statements. The first is an expression statement; the expression consists of a call to the standard library function printf, which generates formatted strings to the standard output device (typically the user’s screen). This call to printf prints a single, constant string, the only unusual feature of which is the two-character sequence \n, which is an escape sequence indicating end-of-line. The second statement is a return statement, by which the function gives up control and optionally returns a value (in this case, the constant 0). A function with the name main is distinguished by being the first function called when a C program begins execution. When main returns, the program terminates, and main’s return value becomes the exit status of the program. (By convention, an exit status of 0 indicates success.) The first line of the program, #include <stdio.h>, calls for the inclusion of some declarations and definitions relating to the I/O functions in the Standard C library; this line is required since the function calls printf.

As a slightly more realistic example, here is a program that prints the first 10 Fibonacci numbers:

#include <stdio.h>

int fib(int);

main()
{
       int i;
       for(i = 1; i <= 10; i = i + 1)
               {
               int f = fib(i);
               printf(“%d\n”, f);
               }
       return 0;
}
/* compute nth Fibonacci number */
int fib(int n)
{
       int i;
       int fibonacci = 0, prev = 1;
       for(i = 1; i <= n; i = i + 1)
               {
               int tmp = fibonacci;
               fibonacci = fibonacci + prev;
               prev = tmp;
               }
       return fibonacci;
}

This program defines two functions: one named main, as before, and one named fib. The line int fib(int) is a forward declaration for the user-defined function fib, indicating that it accepts one argument of type int, and returns a value of type int. The line int i; is a declaration of a local variable, of type int, named i. The line

for(i = 1; i <= 10; i = i + 1)

sets up a loop that steps the variable i through the values 1 to 10 (inclusive). The body of the loop, enclosed in another pair of braces, consists of a declaration of a block-local variable f, a call to the function fib, and a call to printf. In this call to printf, the string to be printed includes the two-character sequence %d, which indicates that the value of another int argument passed to printf, in this case the variable f, should be inserted as a decimal integer (replacing the two characters %d in the output).

The line /* compute nth Fibonacci number */ is a comment; any text between the characters /* and */ is ignored by the compiler. Below that is the definition of the function fib, consisting of several more local variable declarations, another for loop, and a return statement that returns the Fibonacci number computed. One limitation of the function as written is that C’s int type has a guaranteed range of just ±32,767. If this function were to be used to compute Fibonacci numbers past the 23rd, its return type and the types of its local variables would have to be adjusted, probably to the type long int, which has a considerably larger range. (Also, the fib function as written is not terribly efficient; an improved version can be found in section 3.5.3.)


Previous Table of Contents Next