Why do a lot of programming languages put the type *after* the variable name?

Viewed 5445

I just came across this question in the Go FAQ, and it reminded me of something that's been bugging me for a while. Unfortunately, I don't really see what the answer is getting at.

It seems like almost every non C-like language puts the type after the variable name, like so:

var : int

Just out of sheer curiosity, why is this? Are there advantages to choosing one or the other?

12 Answers

"Those who cannot remember the past are condemned to repeat it."

Putting the type before the variable started innocuously enough with Fortran and Algol, but it got really ugly in C, where some type modifiers are applied before the variable, others after. That's why in C you have such beauties as

int (*p)[10];

or

void (*signal(int x, void (*f)(int)))(int)

together with a utility (cdecl) whose purpose is to decrypt such gibberish.

In Pascal, the type comes after the variable, so the first examples becomes

p: pointer to array[10] of int

Contrast with

q: array[10] of pointer to int

which, in C, is

int *q[10]

In C, you need parentheses to distinguish this from int (*p)[10]. Parentheses are not required in Pascal, where only the order matters.

The signal function would be

signal: function(x: int, f: function(int) to void) to (function(int) to void)

Still a mouthful, but at least within the realm of human comprehension.

In fairness, the problem isn't that C put the types before the name, but that it perversely insists on putting bits and pieces before, and others after, the name.

But if you try to put everything before the name, the order is still unintuitive:

int [10] a // an int, ahem, ten of them, called a
int [10]* a // an int, no wait, ten, actually a pointer thereto, called a

So, the answer is: A sensibly designed programming language puts the variables before the types because the result is more readable for humans.

Related