An introduction to an introduction to C

This is the first post in a series on C. The series is meant to complement and supplement a five-day course in basic C that I shall be giving at Cambridge in June 2016. I will post after each lesson if I feel that there is something that needs to be clarified, or if some example code will be useful, but these might not be complete course notes.

About this course

The course is intended for people with some prior experience of programming in an imperative language, although I shall assume no experience with C. I will not dwell on details of the language but will try to move quickly into applications in maths, particularly in numerical computing.

Here is the provisional course structure:

  • Language features. Hello world. Compiling and linking. Declarations and definitions. The types int and double. Functions and return values. The if and else, for, while and switch structures. The break and continue keywords.
  • Memory, pointers and arrays. The concept of memory and why variables must be declared. Declaration and definition of arrays. Declaration of pointers and the use of malloc and free. Arrays as pointers. Passing arrays to functions. Functions with multiple or array outputs.
  • (*) Input and output. The char type. Strings as char arrays, the string.h library. The printf and snprintf functions. Working with files.
  • Mathematics. Variations on the int type. Floating-point calculations. The math.h header. Random number generation with rand.
  • Applications. Euler’s explicit method. A Monte-Carlo integrator and pi calculator. A heat equation solver (Crank-Nicholson).
  • (*) Structures. Application: A simple three-body problem solver.
  • The GNU scientific library. Brief overview of features. Fourier transforms. (*) A KdV solver (split-step pseudospectral).

Before starting

You will need to have a building system set up on your computer. Most Linux distributions come with such a system. If you are on OS X, then two good options are clang and gcc. If you are on Windows, then Microsoft Visual Studio Express is probably the easiest to set up, although clang and gcc are also available.

GNU Make may be useful if you are using clang or gcc. GNU Make is a system for automating the building process, and is widely used, not just in C.

Leave a Reply

Your email address will not be published. Required fields are marked *