What's wrong with C's declarations?

Well first, if you've used C, I'm surprised you have to ask.
Perhaps you've used it so long that it has begun to seem normal to you. :^)

The flip side of this is that you have to deal with old mistakes and with compatibility problems.
For example, I consider the C declarator syntax an experiment that failed.

- Bjarne Stroustrup, in his SlashDot interview on 2/25/2000.


Precedence

Due to precedence, it can be difficult to declare complex types: For instance, what does this declare?:

    int *(*(*(*x)())[10])();
How about this one, declaring the same type?:
    x : pointer to function(): pointer to array[10]
            of pointer to function(): pointer to int;
Sure, its complex too, but you can read the type from left to right and it makes sense.
No grouping parenthesis are ever required (or allowed); precedence has nothing to do with it.

Declaring Multiple variables at Once

With a single declaration, you can declare multiple identifiers, all with different types.

This trips up beginners all the time, people naturally expect:

    int*    first, next, last;

To declare three pointers to integers.

In meme it does, all identifiers in a declaration must share exactly the same type:

    first, next, last: pointer to int;

Compiler-writer hassle

[Too few people care about this, but I'm of the opinion that if a language is difficult for a machine to parse, its not so easy for a person either.]

The name is stuck in the middle of the type, when it would ideally be completely seperate from it.

If the name always appears before the type, there is no difficultly with referencing it in its own declaration.

In C:

    typedef
    struct foo
    {
        int            data;
        struct foo*    next;
    } foo_t;
you are basically forced to name the type twice; it is both "foo_t" and "struct foo".

[C++ is attempting to fix this, as 'class foo' immediately declares 'foo' as a type.]


Part of the reason why Lisp is so neat is that its easy to write a parser/unparser. This leads directly to more non-compiler writers to write code generators and analysis tools; a very good thing, IMO.

While this change doesn't simplify the language to the extreeme, as in Lisp, it is a step in the right direction.

For instance, imagine wanting to write a prototype generator, that takes a source file, and prints the function declaration for each function that is defined. This isn't very easy in C, and its doubly hard if there are K&R functions present.


Go back up to the Meme page.