C
Simple C Examples


C is a directory of C programs which illustrate the use of the C programming language.

Depending on your computer, you might invoke the C compiler by a command like cc, gcc or xlc.

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Related Data and Programs:

CPP is a directory of C++ programs which includes some examples on features of the C++ language.

F77 is a directory of FORTRAN77 programs which includes some examples on features of the FORTRAN77 language.

F90 is a directory of FORTRAN90 programs which includes some examples on features of the FORTRAN90 language.

MATLAB_C is a directory of MATLAB programs which contains examples in which a MATLAB program calls a C function, using MATLAB's MEX facility.

MIXED is a directory of C programs which shows some examples of writing a program partly in C and partly in some other language.

Reference:

  1. Samuel Harbison, Guy Steele,
    C: A Reference Manual,
    Third Edition,
    Prentice Hall, 1991,
    ISBN: 0-13-110933-2,
    LC: QA76.73.C15H38.
  2. Brian Kernighan, Dennis Ritchie,
    The C Programming Language,
    Second Edition,
    Prentice Hall, 1988,
    ISBN: 0-13-110362-8.
  3. Robert Sedgewick,
    Algorithms in C,
    Addison-Wesley, 1990,
    LC: QA76.73.C15S43,
    ISBN: 0-201-51425-7.

Examples and Tests:

ARRAYS is a very simple example of how to set up arrays in C++. In particular, we note that vectors are easy to declare with fixed or dynamic dimensions, but that arrays of dimension 2 or greater aren't easy to declar dynamically. The example shows how to work around by settting up a two dimensional array as a one dimensional vector. Files you may copy include:

FUNCTION_POINTER shows how a variable can be created which can point to a function; the target of the pointer can be changed so that a different function is indicated. Files you may copy include:

HELLO is just a "Hello, world!" program. Files you may copy include:

NOT_ALLOCATED_ARRAYS shows that you should initialize your array pointers to NULL, and reset them to NULL after you delete your arrays. Otherwise, an unallocated or delete array is liable to have a deceptive nonnull value! Files you may copy include:

POISSON solves the Poisson equation on a 2D grid. The program uses dynamically allocated doubly dimensioned arrays, and includes routines to allocate and free the associated memory.

RANDOM_NUMBERS shows how to use the standard library routines SRAND to set the seed and RAND to return random numbers.

SIZES prints out the sizes of various datatypes. Files you may copy include:

You can go up one level to the C source codes.


Last revised on 20 April 2009.