Sign in to follow this  
Azh321

Syntax of Variable Declarations

Recommended Posts

This is a question about the syntax of popular languages. What really is the difference between...
myVar : integer;
and...
int myVar;
Did Pascal (and im guessing other languages?) have a reason for having the type be on the right side of the identifier? Or is it just a matter of taste and C just did the syntax differently?

Share this post


Link to post
Share on other sites
Maybe because they allowed for some complex type expressions. Looking at the Wikipedia page, apparently you can write stuff like this:


var
x: 1..10;
y: 'a'..'z';
z: pear..orange;

var
set1: set of 1..10;
set2: set of 'a'..'z';
set3: set of pear..orange;


If those expressions were on the left, it might be hard or impossible for the compiler to correctly parse it. Also it would be harder for humans to read.

Another nice thing about the type-on-the-right syntax is that you can make it optional to specify a type. That doesn't apply to Pascal, but I assume that's why Actionscript 3 uses it.

Share this post


Link to post
Share on other sites
Pascal evolved from ALGOL, yet doesn't use ALGOL's style for variable declarations. Therefore, you can thank Nicklaus Wirth for the rather idiosyncratic convention. My guess is that, as a mathematician, he saw myVar : integer as pleasingly similar to myVar ∈ Z.

Share this post


Link to post
Share on other sites
Some history.

Quote:
Two ideas are most characteristic of C among languages of its class: the relationship between arrays and pointers, and the way in which declaration syntax mimics expression syntax. They are also among its most frequently criticized features, and often serve as stumbling blocks to the beginner. In both cases, historical accidents or mistakes have exacerbated their difficulty. The most important of these has been the tolerance of C compilers to errors in type. As should be clear from the history above, C evolved from typeless languages. It did not suddenly appear to its earliest users and developers as an entirely new language with its own rules; instead we continually had to adapt existing programs as the language developed, and make allowance for an existing body of code.


Pascal's syntax is generally much less, or perhaps even completely unambiguous.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this