Previous Index Next

An array in C is a collection of data elements of the same data type. The elements in an array are stored in contiguous memory locations. Each element in the array is accessed using its index number, which starts from 0. The size of an array is fixed and determined at the time of declaration.

For example, if you have an array of integers called Age, you can refer to the first element of the array using Age[0], the second element using Age[1], and so on. The index number is an integer that starts at 0 for the first element and increments by 1 for each subsequent element.

array one dimensional

Declaration of Array

To declare an array in C, you use the following syntax:
dataType arrayName[numberOfElements];

For example, to declare an array of integers with 5 elements, you would use:

int Age[5] ;

And to declare an array of floating-point numbers with 30 elements, you would use:

float cost[30];

Note that the size of the array is specified in the square brackets immediately following the array name. The size of the array determines the number of elements it can hold, and it must be a positive integer. The data type of the array elements is specified before the array name, and it must be a valid C data type (such as int, float, char, etc.).

Here is an example of how to declare and use an array in C:


  #include <stdio.h>

  int main() 
  {
      int arr[5]; // declare an integer array of size 5
      arr[0] = 10; // set the value of the first element to 10
      arr[1] = 20; // set the value of the second element to 20
      arr[2] = 30; // set the value of the third element to 30
      arr[3] = 40; // set the value of the fourth element to 40
      arr[4] = 50; // set the value of the fifth element to 50
  
      // print the values of the array
      for (int i = 0; i < 5; i++)
      {
          printf("arr[%d] = %d\n", i, arr[i]);
      }
  
      return 0;
  }

The output of the program would be:

arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50

Initialization of One Dimensional Array

In C, you can initialize an array at the time of declaration using an initializer list. The syntax for initializing an array is:

dataType arrayName[numberOfElements] = {element1, element2, ..., elementN};

For example, to initialize an array of integers with 5 elements, you would use:

int A[5] = {11, 2, 23, 4, 15};

This would initialize the array A with the values 11, 2, 23, 4, and 15, in that order.

Note that you need to enclose the initializer values within curly braces {} and separate them with commas.

It is also possible to omit the size of the array during declaration and let the compiler calculate the size automatically based on the number of elements in the initializer list. For example:

int B[] = {6, 7, 8, 9, 15, 12};

This would initialize an integer array B with 6 elements, with the values 6, 7, 8, 9, 15, and 12, in that order.

Note that when you omit the size of the array during declaration, the compiler calculates the size of the array based on the number of elements in the initializer list. So, in this case, the size of the array is 6.

Referring to Array Elements

In C, you can access individual elements of an array using the array name followed by an index enclosed in square brackets, like this:

arrayName[index]

For example, to access the third element of an integer array numbers, you would use:

int numbers[5] = {10, 20, 30, 40, 50};
int thirdNumber = numbers[2];  // 30

In this example, numbers[2] is used to access the third element of the array numbers, which has a value of 30.

You can also modify the value of an individual element of an array by assigning a new value to it, like this:

numbers[2] = 35;

This would change the value of the third element of the array numbers from 30 to 35.

And you can also use array elements as parameters to functions, like this:

printf("The fifth element of the array is: %d", numbers[4]);

This would print the fifth element of the array numbers, which has a value of 50, to the console.

Using Loop to input and output in Array


  #include <stdio.h>

  int main() 
  {
      int arr[10];
      printf("Enter the array elements: ");
      for (int i = 0; i < 10; i++)
      {
          scanf("%d", &arr[i]);
      }
  
      printf("The array elements are: ");
      for (int i = 0; i < 10; i++)
      {
          printf("%d ", arr[i]);
      }
      printf("\n");
  
      return 0;
  }

In this program, an array arr of size 10 is declared and initialized using a for loop that reads integer values input by the user. Finally, another for loop is used to print out the elements of the array. Note that the scanf() function is used to read input values, and the printf() function is used to print output values.

Example programs

Here are a few examples of programs that use arrays in C

Example 1: Summing the elements of an array

This program creates an array of integers, then loops through the array and calculates the sum of all the elements.


    #include <stdio.h>

    int main() 
    {
      int arr[] = {10, 20, 30, 40, 50};
      int sum = 0;
      
      for (int i = 0; i < 5; i++) 
      {
        sum += arr[i];
      }
      
      printf("The sum of the array is %d\n", sum);
      
      return 0;
    }

Example 2: Finding the maximum element in an array

This program creates an array of integers, then loops through the array and finds the maximum element.


    #include <stdio.h>

    int main() 
    {
      int arr[] = {1, 7, 3, 9, 5};
      int max = arr[0];
      
      for (int i = 1; i < 5; i++) 
      {
        if (arr[i] > max) 
        {
          max = arr[i];
        }
      }
      
      printf("The maximum element in the array is %d\n", max);
      
      return 0;
    }

Example 3: Finding the average of an array

This program creates an array of integers, then calculates the average of all the elements in the array.


    #include <stdio.h>

    int main() 
    {
      int arr[] = {10, 20, 30, 40, 50};
      int sum = 0;
      float average;
      
      for (int i = 0; i < 5; i++) 
      {
        sum += arr[i];
      }
      
      average = (float)sum / 5;
      
      printf("The average of the array is %.2f\n", average);
      
      return 0;
    }

Passing an array to a function

When passing an array to a function, you need to specify the name of the array in the function prototype. Arrays are always passed to functions by reference, which means that any changes made to the array in the function will affect the original array in the calling function.

For example, the following function: 
void print(int A[])
accepts a parameter of type "array of int" called A.
In order to pass to this function an array declared as:
int arr[20];
we need to write a call like this:
print(arr);


Here is a complete example: 

/* This program illustrates array as function parameter */

#include <stdio.h>

void print(int[], int);

int main()
{
    int arr[] = {5, 10, 15};
    print(arr, 3);
    return 0;
}

void print(int A[], int length)
{
    int i;
    for (i = 0; i < length; i++)
    {
        printf("%d ", A[i]);
    }
    printf("\n");
}

Output:

5 10 15

Example program that performs a linear search in an array using a function:


  #include <stdio.h>

  // function prototype for linear search
  int linearSearch(int arr[], int size, int key);
  
  int main()
  {
      int arr[] = {5, 10, 15, 20, 25};
      
      int key = 15;
  
      int index = linearSearch(arr, 5, key);
  
      if (index == -1) 
      {
          printf("Element not found\n");
      } 
      else 
      {
          printf("Element found at index %d\n", index);
      }
  
      return 0;
  }
  
  // function that performs a linear search on an array
  int linearSearch(int arr[], int size, int key) 
  {
      for (int i = 0; i < size; i++) 
      {
          if (arr[i] == key) 
          {
              return i;
          }
      }
  
      // key not found in array
      return -1;
  }  

In this code, we have an integer array arr of size 5 in the main function, along with a key variable that we want to search for in the array. We pass the array, its size, and the key to the linearSearch function using a function call.

The linearSearch function performs a linear search on the input array to find the key. It returns the index of the key if it is found in the array, and returns -1 if the key is not found.

Finally, in the main function, we check the return value of the linearSearch function. If it is -1, we print a message saying that the element was not found in the array. Otherwise, we print a message saying that the element was found at the returned index. In this example, the key value of 15 is found at index 2 of the array.

Previous Index Next

privacy policy