Many programming language use the syntax a[i]
to refer to the i
'th element of an array, sequence, or vector a
- specifically, C and Pascal (from the late 1960s and early 1970s) do this. On the other hand, some earlier languages, like that Fortran (from the 1950s), don't use this convention. Also, I studied a bit of math, and mathematicians use square brackets for intervals, and subscripts for, well, array and matrix subscripting (or regular parentheses if the array is thought of as a function from non-negative integers).
So, my question is: Where/how/in what context did this square brackets for array subscripting develop, and by whom?
Note: Not at all a dupe of this question about the use of curly brackets in C.
-
1Possible duplicate of Why do programming languages, especially C, use curly braces and not square ones?gnat– gnat03/24/2018 11:26:42Commented Mar 24, 2018 at 11:26
-
@gnat: How is this a dupe of that?einpoklum– einpoklum03/24/2018 13:06:48Commented Mar 24, 2018 at 13:06
-
1Related but not a dupe.blaster– blaster03/24/2018 15:11:16Commented Mar 24, 2018 at 15:11
-
1second top answer over there covers the history of square bracketsgnat– gnat03/24/2018 16:04:55Commented Mar 24, 2018 at 16:04
-
1@Paul Typo. You mean xkcd.com/163 (the xkdc domain is registered in China)1201ProgramAlarm– 1201ProgramAlarm03/25/2018 22:05:12Commented Mar 25, 2018 at 22:05
2 Answers 2
The main precursor language to C and Pascal was Algol. The earliest version of that was Algol 58 which used square brackets for array declarations and references.
The reason that Algol used square brackets rather than, for example, parentheses was three-fold:
- because they could. The early IBM keyboards, that Fortran was designed with, only had parentheses. This had changed by the time Algol was being specified.
- experience with Fortran had shown that programmers were often confused with the over use of parentheses so it was seen as an important syntactic change.
- the intent behind Algol was that it would be used for describing algorithms so having it closer to standard mathematical notation made sense.
Note, unlike C, which uses arrays mostly to index memory, Algol allowed both lower and upper index bounds to be specified. Again, this was in keeping with its more mathematical intent. So much so, in fact, that Algol was the de facto language for pseudocode for many years.
-
But are square brackets really closer to standard mathematical notation? When I studied match,
[x]
was used for equivalence classes, not for indication positions within vectors or matrices.einpoklum– einpoklum03/26/2018 10:29:33Commented Mar 26, 2018 at 10:29 -
2@einpoklum More for ranges in the declaration than subscripts or superscripts in the references. But, as they couldn't actually do subscripts or superscripts with the technology of the time, they went for visual consistency. And they weren't aiming for mathematical notion in the way that, say, APL did. They were aiming to clearly express algorithms which is a fairly narrow branch.Alex– Alex03/26/2018 10:38:50Commented Mar 26, 2018 at 10:38
-
1Oh, also, square brackets were used for intervals. I guess that's like ranges in an array, in a sense. Also - you're sure Algol is where this custom started?einpoklum– einpoklum03/26/2018 12:42:15Commented Mar 26, 2018 at 12:42
-
@einpoklum I'm sure that the use of square brackets in C and Pascal came from Algol. The lineage is fairly well documented. If there was a predecessor to Algol 58 that the designers took the syntax from, I'm unaware of it. It's unlikely given the timing but not impossible. Algol 58 wasn't implemented prior to Algol 60 so it's possible that an unimplemented language was an inspiration.Alex– Alex03/26/2018 13:07:49Commented Mar 26, 2018 at 13:07
This is an interesting read: https://en.m.wikipedia.org/wiki/Bracket
The following are my own observations.
The C designers took great care adopting the meaning of characters and constructs as they had been used for hundreds of years in regular written language. And doing so, they had to work with the quite limited subset of ASCII characters. In western language, more grouping characters are used in written text but these did just not make it into ASCII.
In regular language, the meaning of parentheses is providing side information without interrupting the main message. This makes sense when calling a procedure: the main message is the action to be performed and the sub-info are the arguments.
Curly braces are used for grouping. "This collection of words belong together, are to be set apart from the rest". So it makes sense to use them for blocks of code, compound statements.
Square brackets are used for insertions in text that were not part of the original, to clarify the original text and provide context. Or to indicate omission with ellipsis: [...]. "She [the queen] was not amused". In computer programming it makes a little sense to use the bracket for array indexing because it is a kind of context being provided. "Array? What array? Well, specifically that element." But even if the original meaning does not fully cover the use in a computer language, there aren't too many other options left in ASCII.
So I would say it is a combination of prior art and what was available at the time that lead to the use of square brackets for array indexing.
[edit because of Alex's comment]
According to this source, ASCII was introduced in 1963 and got governmental approval in 1968. Development of the C language did not start until 1969, according to this wiki. So it seems likely Mr. Ritchie kept an eye on this new widely supported standard called ASCII when picking his characters.
Please also see the other answer(s) making a case for Algol, "the mother of many languages", being a strong influence.
-
I think I get what you're saying but ASCII wasn't codified until nearly a decade after C was designed. A much more direct impact was the keyboard of the PDP-11 that C was designed for. It makes sense to use characters that it supports. The use of square brackets for arrays was directly inspired from Algol via BCPL and B. Similarly for Pascal. Wirth implemented Algol-W prior to designing Pascal.Alex– Alex03/26/2018 13:24:19Commented Mar 26, 2018 at 13:24