CS322: Introduction to the C Programming Language

Materials:

I. History and Description of C

A. History

B. Dialects and Standards

II. Syntax of C

A. A C source file consists of a series of declarations and definitions.

  1. type declarations - used to declare new types.

    		int my_array[20];
    
    		struct student {
    		    char name[20];
    		    float gpa;
    		};
    		

    (my_array is an array consisting of 20 integers; struct student is the name of a record type.)

  2. variable declarations - used to declare variables and (in ANSI-C) as one way of declaring constants. C variables can be initialized when they are declared.

    		int i = 0;
    		

  3. function declarations/definitions

    • A function declaration spells out the interface to a function, but does not give its body. It consists of a function prototype followed by a semicolon.

      			int f( int );
      			

    • A function definition consists of a function prototype followed by the function body.

      			int f( int i )
      			{
      			    return i * i; 
      			}
      	    		

    • Note that C does not distinguish between functions and procedures. The C analog to a Pascal procedure is a function that doesn't return any value. In ANSI-C, this is handled by declaring the "procedure" as a function returning type void.

      			void p( void );
      	    		

  4. There is no special provision for a main program block, as in Pascal. Instead, every C program must contain exactly one function whose name is main - that function is the main program. It can occur anywhere within the source file(s).

  5. The C rules for placement of declarations differ in some important ways from those of Pascal.

    • Declarations can appear in any order or mixture - subject to the requirement that something must be declared before it is used.

    • Variables and functions (but not types) can be declared more than once, provided the declarations are consistent. However:

      1. Only one declaration for a given variable can initialize it. This is called its defining declaration.

      2. A function can only be defined once.

    • Any C compound statement (sequence of statements enclosed in braces) can begin with one or more local variable declarations. This includes not only the body of a function, but also any nested compound statements within the body.

    • C functions cannot contain nested declarations or definitions of other functions. All C functions are declared at "top level".

B. Large C programs are typically written as a collection of separate source files, each declaring related functions and data, which are compiled separately.

C. A complete C implementation consists of the C compiler proper, plus a C-preprocessor that handles certain pre-processor directives.

D. Some examples: HANDOUT - C PROGRAM DEMONSTRATIONS

E. Syntax details: C vs Pascal HANDOUT

III. The C Run Time Library

A. As was noted earlier, one of the main strengths of the C language is the existence of a large, standardized library of functions. These functions perform a wide variety of tasks, including:

  1. All input-output operations

  2. Mathematical functions

  3. Character string operations

  4. Memory management and other system-related tasks

  5. (On windowed systems) Screen window management.

B. Part of the ANSI standardization effort has been a standardization of this library. This is an important consideration in making C programs truly portable. Unfortunately, though, some system dependencies still exist.

C. HANDOUT - Excerpts from Unix documentation. Note documentation format, use of standard headers.


$Id: c.html,v 1.2 2000/01/07 17:03:32 senning Exp $

These notes are based on a set of notes by Prof. R. Bjork, Gordon College and The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie, Prentice-Hall, 1978.