Array Declarations in Various Programming Langauges
In C++, we declare an array by indicating the number of data items in brackets:
array of 20 integers, named cat:
int cat[20];
an array of 12 characters, named dog:
char dog[12];
In C++, the items in an array are numbered 0 through N-1, where N is the size of the array. In the above examples,
the 20 elements of cat are cat[0] through cat[19]
the 12 elements of dog are dog[0] through dog[11]
Every programming language supports arrays. But the ways in which they do so vary widely. The table below compares how integer arrays are declared in some well-known programming languages:
| Language | Typical Array Declaration | Valid Subscripts |
| FORTRAN | INTEGER MYARRAY(10) | 1 through 10 |
| BASIC (most dialects) | Dim myArray(10) As Integer | 0 through 10 |
| Visual BASIC 4.0 | Dim myArray(3 to 11) As Integer | 3 through 11 |
| Pascal | var myArray: array[0..10] of integer | 0 through 10 |
| Pascal | var myOtherArray: array[3..8] of integer | 3 through 8 |
| C, C++ | int myArray[10]; | 0 through 9 |
| Java | int myArray[] = new int[10]; | 0 through 9 |
Note the variety. FORTRAN arrays start with index 1; FORTRAN arrays are said to be 1-based. C, C++, and Java arrays start with index 0; these arrays are said to be 0-based. In most dialects of BASIC, arrays are 0-based and the number you write in parentheses is the highest index allowed; so if you write N in the parentheses, the array contains N + 1 elements. In Pascal and in Visual BASIC 4.0, you can base your arrays any way you like! In C, C++, and Java, the number you write is the number of elements; so if you write N in parentheses, the highest index allowed in N - 1. Finally, some languages use parentheses whereas others use brackets.
You DO NOT need to know all these details (except for C++ of course). But you should be aware that there is great variation from one language to another (and in the case of BASIC, from one dialect to another).