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.


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


Directories related to C are available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version.

Related Data and Programs:

C_ARRAYS, C programs which illustrate the use of vectors, matrices and tensors.

C_CALLS_F77, C programs which call a FORTRAN77 subroutine.

C_CALLS_F90, C programs which call a FORTRAN90 subroutine.

C_FILES, C programs which illustrate the use of files.

C_INTRINSICS, a C program which illustrates the use of intrinsic functions supplied by the C language.

C_OPERATORS, a C program which illustrates the use of some of the operators supplied by the C language.

C_RANDOM, C programs which illustrate the use of C's random number generator routines.

MATLAB_CALLS_C, MATLAB programs which call a C function, using MATLAB's MEX facility.

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


  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:

ARRAY_APPEND demonstrates how one new value can be appended to an existing array.

ARRAY_RETURN demonstrates how a function can call another function which creates and returns several arrays in its argument list. To do this requires some tricky use of pointers.

BIG_INTS_REAL shows what can go wrong when you try to move large integer values into and out of real variables.

CHARACTER_ARG demonstrates how a C function can return character information through its argument.

CHARACTER_ARITHMETIC demonstrates that characters are really unsigned short ints (numbers between 0 and 255) so you can do arithmetic with them.

DYNAMIC_ARRAY_2D demonstrates how a 2D array can be allocated dynamically.

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.

HELLO is just a "Hello, world!" program.

LINKED_LIST demonstrates how to use a linked list in order to be able to store an unknown and increasing number of objects of the same type, typically a C "struct". In this case, we want to store a size (integer), a real vector, and a real matrix, for each object, and we don't know how many objects we will have. Once we have stored them all, we simply want to be able to run through the list and examine each object.

LINKED_LIST_VECTOR creates a vector of N linked lists, each of an unknown length. Using the vector, we can immediately examine any particular list without traversing the previous ones. The rows of a sparse matrix might be stored in this way, for instance.

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!

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.

SCANF_DEMO demonstrates how scanf(format,address) can be used to read data from the user.

SIZES prints out the sizes of various datatypes.

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

Last revised on 14 May 2018.