Array in C Tutorial with Detailed Example

In this tutorial, we will discuss how the array in C works. Also, we will discuss the declaring arrays, initializing arrays, Accessing Array Elements, and Arrays in Detail.

Size of array in C

How to define an array?

In C language, an array is a variable that can hold several values. For an example, if they want to store 350 integers, we can make an array for it.

int balance [350];

How to Declare the C array?

For the declaration of an array in C, a programmer can define the type of the elements, as well as how many elements an array will be required.

dataType arrayName[arraySize];

This is known as a single-dimensional array. The arraySize should be a constant integer greater than zero and the type must be a valid data type in C. Here is an example, a declaration of the 10-element array which is called balance.

double total[30];

Note: If it is declared, an array’s size and type cannot be modified.

More about C Programming Tutorial.

Access Array Elements

We can access the elements through an array by indices.

We assume, we will declare an array number on the top. The first element is number[0], the second element is number[1] and etc.

Initialize an Array in C

Few Features:

  • The first index has an array which is 0 and is not 1. For example, the first element is which is the number(0)
  • When the size of an array is x, which is to access the last element, we can use the x-1 index. for example a number(4).
  • We assume the beginning address of number(0) is 5150e. Furthermore, the address of the number(1) will be 5154e. Additionally, the next number(2) will be 5158e and so on.
  • This is due to the reason that a float’s size is 4 bytes.

How to initialize an array in C?

To initialize an array in C its either in one by one or using a single statement.

Example in initializing an array:

int number[5] = {10, 21, 13, 41, 15};

We will also initialize an array like this.

int number[] = {10, 21, 13, 41, 15};

Over here, we haven’t defined the size. Hence, given that we initialize it with 5 elements, the compiler will know of its size.

An example is here:

Number[0] is equal to 10
Number[1] is equal to 21
Number[2] is equal to 13
Number[3] is equal to 41
Number[4] is equal to 15

Changing the Value of an Array Elements

int number[5] = {10, 21, 13, 41, 15}

// Create the value of the third element to -1
number[2] = -1;

// Create the value of the fifth element to 0
number[4] = 0;

The Input and Output in Array Elements

This is on how to collect user input and save it in an array element.

// This is to take input and store it in the 3rd element
​scanf("%d", &number[2]);

// This is to take input and store it in the nth element
scanf("%d", &number[n-1]);

This is how we can print an individual element of an array.

// This is to print the first element of the array
printf("%d", number[0]);

// This is to print the third element of the array
printf("%d", number[2]);

// This is to print nth element of the array
printf("%d", number[n-1]);

Example of Array Input and Output

// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

#include <stdio.h>

int main() {

  int num[10];

  printf("Enter 1 integers: ");

  // taking input and storing it in an array
  for(int x = 0; x < 10; ++x) {
     scanf("%d", &num[x]);
  }

  printf("Displaying integers: ");

  // printing elements of an array
  for(int x = 0; x < 5; ++x) {
     printf("%d\n", num[x]);
  }
  return 0;
}

Output:

Enter 10 integers: 1 2 3 4 5 6 7 8 9 10
Displaying integers:
1
2
3
4
5
6
7
8
9
10

Explanation of the code:

In the code above, we used for loop to gather 10 user inputs and store them in an array. These elements are shown on the screen by applying another for loop.

Let’s see the other example of an array in C.


#include <stdio.h>

int main() {

  int grades[5], x, y, sum = 0;
  double average;

  printf("Enter number of grades: ");
  scanf("%d", &y);

  for(x=0; x < y; ++x) {
    printf("Enter grades%d: ",x+1);
    scanf("%d", &grades[x]);
          
    sum += grades[x];
  }

  average = (double) sum / y;

  printf("Average = %.2lf", average);

  return 0;
}

Output:

Enter number of grades: 5
Enter grades1: 87
Enter grades2: 87
Enter grades3: 86
Enter grades4: 89
Enter grades5: 65
Average = 82.80

In the example above, we calculate the average of y numbers which is entered by the user.

Access elements outside of its bound!

We assume that we declared an array of 10 elements. For example:

int ArrayName[20];

You can access the array of elements from testArray[0] to testArray[9].

If we try to access ArrayName[20]. The element is not accessible. This could result in unexpected output (undefined behavior).

The program may sometimes work correctly and other times you may encounter errors.

Furthermore, we must never access the elements of an array on the outside of its bound.

Conclusion

In this tutorial on C Array, we have discussed and learned almost all the Array in C with proper explanations of examples.

The tutorials begin with a brief introduction to Arrays in C followed by details of the different examples of array in C.

1 thought on “Array in C Tutorial with Detailed Example”

Leave a Comment