C is not, of course, an object oriented language and does not even have any discernible features of one. The three core characteristics of object oriented programming are frequently stated to be encapsulation, inheritance and polymorphism, but a more fundamental characteristic is the combining of data (or properties) and the functions (or methods) which work on that data into a single entity which can be used to access both. This can be simulated in C by adding function pointers to a struct. The nuts and bolts of doing so are not as slick as in a real OOP language such as C++ and frankly look a bit clunky, but in this post I will write a short demonstration of the principle. Whether it is actually worthwhile is a moot point!
Soundex is one of a number of phonetic algorithms, assigning values to words or names so that they can be compared for similarity of pronounciation. It is probably the best know such algorithm as it is built in to most major RDBMSs, as well as PHP and other languages.
It doesn’t take much thought to realise that the whole area of phonetic algorithms is a minefield, and Soundex itself is rather restricted in its usefulness. In fact, after writing this implementation I came to the conclusion that it is rather mediocre but at least coding it up does give a better understanding of how it works and therefore its usefulness and limitations.
Wikipedia has a surprisingly brief article on the topic Soundex on Wikipedia which you might like to read.
In a number of previous posts I have used more than one source code file, one for the main function and others for functions used by main. This is purely to make the source code easier to handle than would be the case if all the code were in one large source file.
In this project I will go beyond separating source code into different files by compiling the additional files into static libraries which can be copied to a central location along with their header files and used by many programs, in exactly the same way as the standard library is used.
This is a short post to demonstrate the use in C of a technique which is common in other languages – using callbacks as function parameters to tell the function what to do in certain circumstances. In this project I will write a function which will simulate retrieving data from a database and then call one of the two functions passed to it as pointers, the first if the data retrieval was successful and the second if there was an error.
One of the most useful bits of number-crunching you can do with data is to calculate the probability distribution, in the earnest hope that it will be a reasonable fit for one of the recognised distributions such as the normal distribution. In this project I will write a small C library to calculate the normal distribution for a given data set.Continue reading
The shortest distance between two locations on the surface of Earth (or any planet) is known as the Great Circle Distance. Except for relatively short distances these cannot be measured on a map due to the distortion and flattening necessary to represent a sphere on a flat plane. However, the calculation of the Great Circle Distance from two coordinates is simple, although I suspect generations of midshipmen might not have agreed. In this post I will write a short program in C to calculate the distance from London to various other cities round the world.Continue reading
The majority of data can easily be plotted on a graph with equal intervals on the axes, for example 1, 2, 3 or 100, 200, 300 etc.. Some data, typically that which increases or decreases exponentially, cannot comfortably be graphed on such a scale without squashing the data up so much at one end that it becomes incomprehensible. The solution to this problem is to use a logarithmic scale.Continue reading
A very quick micro-post just to say that I have pushed the source code for all my posts to date to Github. The URL is
The source code for all future posts will also be on Github so feel free to follow me if you wish.
Writing code to read or write text files can be tricky because the exact contents of a file cannot be viewed in a text editor if the file contains non-printable characters such as line feeds or carriage returns. This simple utility program will take a filename as a command line argument and print out its exact contents, including descriptions of any non-printable or whitespace characters.
C99 introduced a new addition to the standard library to support complex numbers. The area of complex numbers is a vast one so in this post I will just give an overview of the C types and some of the associated functions, as well as writing a function to draw an Argand diagram using SVG.Continue reading