# C intro 5. array

When we use a computer, it is usual to handle many data (numerical values). For handling two or three numbers, a simple calculator is enough! In the handling of many data, it is convenient to treat individual data with a serial number (like handling students' math test results by using their students' ID number). Not only in C but in many programming languages such things are possible. Such variables (reference of variables with serial numbers) are called "array variables".

Suppose we treat data 7, 1, 8, 5, we may write our program as:

```   # include <stdio.h>
main(){
int a, b, c, d;
a=7;
b=1;
c=8;
d=5;
....
}
```

which give a label (name) for each data. However, if the number of data becomes more than 26, the alphabet character dpletes! Moreover, you may sometimes wonder "which data does d represent?". We can improve by the following approach:

```   # include <stdio.h>
main(){
int a[4];
a[0]=7;
a[1]=1;
a[2]=8;
a[3]=5;
....
}
```

Watch a[ ]. It is a new concept which is called "array variables". It stores and represents a data by labeling a serial number in [ ] which means how many data before that data. For example, a[0] is the first data because it has no data (0 data) before it.

When you want to use an array variable, first you must declare it. In this example we did it by

```   int a[4];
```

where, "int" means integer and [4] means we use 4 data, which are a[0], a[1], a[2], a[3]. Be careful that we do not have a[4].

Now time for exercise. Let's make a program which gets four integers and display them in reverse order.

```  1 /* array.c */
2 /* compile: \$ gcc array.c -o array */
3 /* 2017/02/18 K. Nasahara */
4 # include <stdio.h>
5 main(){
6 int a[4];
7 int i;
8 for (i=0; i<4; i++){
9    scanf("%d", &a[i]);
10   }
11 for (i=3; 0<=i; i=i-1){
12    printf("%d\n", a[i]);
13   }
14 }
```

You save this source code with a name "hairetsu.c", compile, and run.

```\$ gcc hairetsu.c -o hairetsu
\$ ./hairetsu
7
1
8
5
5
8
1
7
```

Exercise 5-1 Try to replace "i<8" in line 8 by "i<5". What happens? Perhaps an error. Why?

Exercise 5-2 Try to replace "i<8" in line 8 by "i<5" and at the same time, replace a[4] by a[5] in line 6. What happens?

As we have learned, we cannot access to a[4]. The program gives an error if we try to access a[4] which does not exist. In summary,

• The index of an array variable starts from 0. Not from 1.
• Therefore, the maximum possible index is the value one smaller than the array size.
• Access to an array data which does not exist gives an error.

This error is very often in many computer languages. Please watch!!

### Do not leave a magic number in the middle of the source code!

The program "array.c" works fine, but it is not a good program in terms of "programming style". More specifically, it is not easy to understand and modify the program. For example, if you are asked to modify so that it can handle 5 integers instead of 4, you must change multiple places in the source code. The following style is much better:

```   /* array2.c */
/* compile: \$ gcc array2.c -o array2 */
/* 2017/02/18 K. Nasahara */
# include <stdio.h>
# define N 4   // number of integers
main(){
int a[N];
int i;
for (i=0; i<N; i++){
scanf("%d", &a[i]);
}
for (i=N-1; 0<=i; i=i-1){
printf("%d\n", a[i]);
}
}
```

Then you just change the value N.

In this example, "4" is an example of a magic n umber, which is a parameter value to control the program. Each magic number has its own unique role. You should make it clear by summarizing them at the head of the program and by giving them some understandable names and comments.

[Go back to C language introduction]