Why C Is Not My Favourite Programming Language

Ahem.

C is sometimes castigated for the syntax of its declarations, particularly ones that involve pointers to functions. The syntax is an attempt to make the declaration and the use agree; it works well for simple cases, but it can be confusing for the harder ones, because declarations cannot be read left to right, and because parentheses are over-used. The difference between int *f() and int (*pf) illustrates the problem

[…]

char (*(*x[3])())[5]

x: array[3] of pointer to function returning pointer to array[5] of char

— Kernighan & Ritchie, The C Programming Language, 2nd Edition, Chapter 5 (Pointers and Arrays), section 5.12 (Complicated Declarations).

Case closed.

.

.

.

(Sorry that the Category for this posting is “C language” rather than C; as far as I can make out, WordPress ignores !isalnum() characters when determining uniqueness, and won’t let me add a “C” category because I already have “C++”.)

6 responses to “Why C Is Not My Favourite Programming Language

  1. Why I dont think clojure is going to my favourite language:


    (defn parse-line [line ary]
    (let [fields (dumbest-split line \space ary)
    status (aget fields 8)
    bytes (aget fields 9)
    #^String ref (aget fields 10)]
    (when (and (= (aget fields 5) "\"GET")
    ('#{"200" "304" "404"} status))
    {:client (aget fields 0)
    :url (aget fields 6)
    :status status
    :bytes (if (= bytes "-") 0 (Long/parseLong bytes))
    :ref (.substring ref 1 (dec (count ref)))})))

  2. Like you’re ever gonna need that one..

  3. :) C “IS” the language. Far from being perfect, but really outstands others.

    The sample you provide has some good points, working with function pointers can be big pain, but don’t forget you have preprocessor to ease it.

    #define is correctly used can save a lot of headaches, however when using preprocessor’s macros we’ve got to be pragmatic, in order to avoid #define nightmare macros. (each macro shall be an expression)

    C despite it’s 40 years still rocks. You can learn it in a few days (syntax is not too complex and the standard lib is pretty easy)

    Readability can be a problem… just think about the Duff’s Device ;)

  4. Yeah, the function pointer declarations are pretty terrible to look at and makes me wish they had perhaps added special syntax to deal with them.

    When creating “complex” or compound types that involve function pointers, I always break the declaration into more manageable types with typedef’s. It tends to make the intention clear.

    My biggest annoyance with C (also shared with C++) is maintaining consistency in the redundant information found in the declaration (the header file) and definitions (in the source file). I know, various programming environments help deal with that, but it’s still a deficiency in the language as far as I’m concerned.

  5. Even more than the declarator syntax (and note that later languages written by the inventors of C fixed it, so even they think it was a mistake: Pike and Thompson say as much in the Google Go FAQ), array->pointer decay was a serious mistake. I’ve programmed in C for several hours of virtually every day for the last two decades, but even so I’m scared to use multidimensional arrays, particularly in conjunction with cv-quals: and ‘const’ is far more useful than a multidimensional array, since ‘const’ finds bugs, while multidimensional arrays introduce them.

    Rather than arrays of any sort, it’s generally better to use a decent ADT modelling some sort of unboundedly long list (and thankfully thanks to structure hiding it’s easy to do opaque types in C: you can even implement polymorphic types with a bit of grit. But most people use frameworks like GObject that provide decent data structures, so they don’t need to write them themselves).

  6. I once suggested to Dennis Ritchie, at a convention, that “C” would be a lot easier to understand if indirection was a suffix rather than a prefix operation as it was in Pascal.

    [which would make that something like “char x[3]^()^[5]” or “x is an array of three pointers to functions returning pointers to array of 5 chars”]

    His response was that they had thought of the same idea back in the ’70s, but there were by that time more than four locations using “C” and such a major change to the language would be disruptive.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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