Array in C

You will learn how to work with arrays in this tutorial. With the aid of examples, you will discover how to declare, c initialize array, and access array elements.

An array is a type of variable that can store several values. For example, if you wanted to store 100 integers, you could make an array for it.

Definition of Array

The definition of an array in C is a way to group together several items of the same type. These elements may have user-defined data types like structures, as well as standard data types like int, float, char, and double. All of the components must, however, be of the same data type in order for them to be stored together in a single array. The items are kept in order from left to right, with the 0th index on the left and the (n-1)th index on the right.

How may an array be declared?

Array Syntax is very easy to declared. You just need to understand the statement given below.

Declaration of array syntax:

dataType array_name[arraySize];

Example:- Declare an array

int student_marks[20];
char student_name[10];
float numbers[5];

In the last example, we declared an array “numbers” of the type int. Its length is 5. In other words, it can store 5 integer numbers.

It’s crucial to remember that once an array is declared, its size and type cannot be modified.

Access Array Elements

An array’s items can be accessed using their indices.
Let’s say you made the above array declaration. mark[0] is the initial element, followed by mark[1] and so on.

Few keynotes:

  • Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
  • If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]
  • Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
  • This is because the size of an int address is 4 bytes.

Discover The C Initialize Array

An array can be initialized while being declared. For instance,

int mark[5] = {19, 10, 8, 17, 9};

C initialize array can be done using this formula:

int mark[] = {19, 10, 8, 17, 9};

Here, the size is left unspecified. However, because we initialize it with 5 elements, the compiler is aware that it has a size of 5.

Here,

mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9

Change Value of Array elements

Array is very flexible for changing the value of its elements. You can simply change any element as show in the example below:

int mark[5] = {19, 10, 8, 17, 9}

// make the value of the third element to -1
mark[2] = -1;

// make the value of the fifth element to 0
mark[4] = 0;

Input and Output Array Elements

Here’s how to take user input and store that value in an array element.

// take input and store it in the 3rd element
scanf("%d", &mark[2]);

// take input and store it in the ith element
scanf("%d", &mark[i-1]);

How to print an individual array element is shown below.

// print the first element of the array
printf("%d", mark[0]);

// print the third element of the array
printf("%d", mark[2]);

// print ith element of the array
printf("%d", mark[i-1]);

Code Implementation of Taking Array Input/Output

#include <stdio.h>

int main() {

  int values[5];

  for(int i = 0; i < 5; ++i) {
     scanf("%d", &values[i]);
  }

  printf("Displaying integers: ");

  for(int i = 0; i < 5; ++i) {
     printf("%d ", values[i]);
  }
  return 0;
}

Output

Displaying integers: 2 4 6 8 10

Here, a for loop has been used to collect 5 user inputs and store them in an array. These elements are then shown on the screen by using another for loop.

Code Implementation To Calculate Average

#include <stdio.h>

int main() {

  int marks[10], i, n, sum = 0;
  double average;
  scanf("%d", &n);

  for(i=0; i < n; ++i) {
    scanf("%d", &marks[i]);
          
    // adding integers entered by the user to the sum variable
    sum += marks[i];
  }

  // explicitly convert sum to double
  // then calculate average
  average = (double) sum / n;

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

  return 0;
}

Input

5
2 4 6 8 10

Output

Average = 6.00

Here, we’ve calculated the average of the user-entered n numbers.

Obtain components outside of its bounds!
Let’s say you specified a 10 element array. Imagine that,

int testArray[10];

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

Say that you attempt to access testArray[12]. The component is not accessible. This could result in unexpected output (undefined behavior). You might encounter errors from time to time, and other times your program might function properly.

As a result, you must never access an array’s items outside of their boundaries.

Conclusion
Hoping this article has given you the clear idea about what is array, array syntax, c initialize array and lot more about array in c. Practice more to push your limits for the array topic in your coding journey.

Leave a Reply

Your email address will not be published. Required fields are marked *