Array In C Programming Language

Array In C Programming Language

You will learn about Array in C Programming With the aid of examples, you will discover how to declare, initialize, and access array elements.

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable.

To make an array, supply the data type (such as int) and the array name in square brackets [].

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.

An array is a group of data elements of the same type that are stored in contiguous memory regions.

Arrays are derived data types in the C programming language that can store primitive data types such as int, char, double, float, and so on. It can also store a collection of derived data types like pointers, structures, and so on.

The array is the most basic data structure since each data element may be accessed at random by using its index number.

If you need to keep comparable elements, a C array is useful. For example, if we want to record a student’s marks in six subjects, we don’t need to declare separate variables for each subject’s values. 

Instead, we can create an array that stores the marks in each subject in contiguous memory locations.

int data[100];

How to declare an array?

dataType arrayName[arraySize];

For example,

float mark[5];

Here, a floating-point type array named mark has been declared. Its dimensions are 5. It can therefore store five floating-point values.

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

Access Array Elements(Array in C Programming)

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.

Notes

  • The first index of an array is 0, not 1. Mark[0] is the initial element in this example.
  • The n-1 index is used to reach the last element of an array if its size is n. As an illustration, mark[4]
  • Assume mark[0initial ]’s address is 2120d. The mark[1address ]’s will then be 2124d. Mark[2address ]’s will be 2128d similarly, and so on.
  • This is due to the fact that a float’s size is 4 bytes.

How to an array initialized?(Array in C Programming)

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

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

An array can also be initialized like this.

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 an array’s element value(Array in C Programming)

int mark[5] = {19, 10, 8, 17, 9}
// make the value of the third element -1
mark[2] = -1;
// make the value of the fifth element 0
mark[4] = 0;

How to take an Input and Output for an Array Element(Array in C Programming)

Here’s how to collect user input and save it 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 a specific 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]);

Example 1: Array Input/Output (Array in C Programming)

// 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 values[5];

  printf("Enter 5 integers: ");

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

  printf("Displaying integers: ");

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

Output

Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3

We’ve used a for loop to take 5 user inputs and store them in an array. The items are then presented on the screen using another loop.

Example 2: Calculate Average

// Program to find the average of n numbers using arrays

#include <stdio.h>

int main() {

  int marks[10], i, n, sum = 0;
  double average;

  printf("Enter number of elements: ");
  scanf("%d", &n);

  for(i=0; i < n; ++i) {
    printf("Enter number%d: ",i+1);
    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;
}

Output

Enter number of elements: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39.60

In this case, we calculated the average of n numbers entered by the user.

Access items that are outside of its bounds!

Assume you declared a 10-element array. Assume that

int testArray[10];

The array elements are accessible from testArray[0] to testArray[9].

Assume you attempt to access testArray[12]. The element is currently unavailable. This may result in unexpected output (undefined behavior). Sometimes you’ll get an error, and other times your program will work fine.

As a result, you should never access an array’s elements outside of its bounds.

Multidimensional arrays

You learned about arrays in this session. These are known as one-dimensional arrays.

The next tutorial will teach you about multidimensional arrays (array of an array).

you may like :

Data Types In C with Example

Leave a Reply