Is it possible to fix C?

Many languages have evolved over time to fix their inadequacies. Fortran moved from unstructured to structured programming by nixing structures like the arithmetic if and all those nasty goto statements. C has never done this – sure features have been added but issues like the dangling else, and fall-through in switch have never been remedied. Is it possible to fix these things, or is it too late?

It may likely be too late. The evolution of languages such as Apple’s Swift may displace others such as Objective C. A good example is the switch statement. In C-like languages it suffers from fall-through, where unless a break statement is specifically applied, one statement will flow through to the next – a common source of problems. Here is an example:

switch (n) {
    case 1: code1;
    case 2: code2;
    case 3: code3;
    default: code4;

In this case, if n=1, code1 will be executed, and then the code will fall-through into code2, which will be executed. This is obviously a problem because it is easy to miss these break statements. Conversely, in Swift, fall-through does not occur (unless you specifically specify it) – the entire switch statement finishes its execution as soon as the first matching case is completed, without requiring an explicit break statement. But the statement goes further… it allows for multiple matches for a single case statement, and interval matching (as well as a bunch of other cool stuff).

switch n {
    case 0:
    case 1,2,3,4,5,6,7,8,9:
        println("single digits")
    case 10..99:
        println("double digits")

Swift also allows break statements in the switch, and a keyword fallthrough, which mimics the behaviour in C. Swift has clearly improved the usability of the switch statement.

Watch out C… the party might be over!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s