Categories
b. Derived Data Type

Arrays

An array is a group of similar kinds of finite entities of the same type. These entities or elements can be referred to by their indices respectively. The indexing starts from 0 to (array_size-1) conventionally.  An array can be one-dimensional, two-dimensional, or multidimensional.

Syntax

data_type arr_name[size];

Description of the syntax

  • data_type: This is the data type that specifies the type of elements to be stored in the array. It can be int, float, double, and char.
  • array_name: This is the name of the array. To specify the name of an array, you must follow the same rules which are applicable while declaring a usual variable in C.
  • size: The size specifies the number of elements held by the array. If the size is n then the number of array elements will be n-1.

Example

The following example illustrates the array data types in C.

#include <stdio.h>

{

int idx, element;
    // initialize an array.

int my_array[10] = {10, 0, 29, 8, 52, 14, 16, 100, 2, 27};

printf("Enter element to be searched:\n");

    // input element to be searched.

scanf("%d", &element);

    // traverse the array to search the element.

for (idx = 0; idx <= 9; idx++)

{

if (my_array[idx] == element)

{

            // print the index at which

            // the element is found.

printf("Element found at idxex %d", idx);

break;

}

}

    // if the element is not found.

if (idx == 10)

{
printf("\nElement not found!");

}

return 0;

}
Data_Types_in_C_6

The above example is to check if the input element exists in the array or not. Here, we have declared an integer type array for size 10. We are iterating over the array and checking if the given element is found in the array. 

Leave a Reply

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