One – Dimensional Arrays

Array – In the C programming language an array is a fixed sequenced collection of elements of the same data type. It is simply a grouping of like type data. In the simplest form, an array can be used to represent a list of numbers, or a list of names. Some examples where the concept of an array can be used:

  • List of temperatures recorded every hour in a day , or a month, or a year.
  • List of employees in an organization
  • List of products and their cost sold by a store
  • Table of daily rainfall data

Since an array provides a convenient structure for representing data, it is classified as one of the data structures in C language.

There are following types of arrays in the C programming language –

  1. One – dimensional arrays
  2. Two – dimensional arrays
  3. Multidimensional arrays

1D Arrays in C language –  A list of items can be given one variable name using only one subscript and such a variable is called single sub-scripted variable or one dimensional array.

Declaration of 1D Arrays –

Like any other variable, arrays must be declared before they are used so that the compiler can allocate space for them in the memory. The syntax form of array declaration is –

                                type variable-name[size];
Ex -
      float height[50];
      int groupt[10];
      char name[10]

The type specifies the type of the element that will be contained in the array, such as int, float, or char and the size indicates the maximum number of elements that can be stored inside the array. Also the C programming language treats character strings simply as arrays of characters.

Now as we declare a array

int number[5];

Then the computer reserves five storage locations as the size of the array is 5 as shown below –


Initialization of 1D Array

After an array is declared, it’s elements must be initialized. In C programming an array can be initialized at either of the following stages:

  • At compile time
  • At run time

Compile Time initialization

We can initialize the elements of arrays in the same was as the ordinary variables when they are declared. The general form of initialization of array is:

                    type array-name[size] = { list of values };

The values in the list are separated by commas. For ex, the statement

int number[3] = { 0,5,4 };

will declare the variable’ number’ as an array of size 3 and will assign the values to each element. If the number of values in the list is less than the number of elements, then only that many elements will be initialized. The remaining elements will be set to zero automatically.

Remember, if we have more initializers than the declared size, the compiler will produce an error.

Run time Initialization

An array can also be explicitly initialized at run time. For ex – consider the following segment of a C program.

            scanf(" %d ", &x[i] );

Above example will initialize array elements with the values entered through the keyboard. In the run time initialization of the arrays looping statements are almost compulsory. Looping statements are used to initialize the values of the arrays one by one by using assignment operator or through the keyboard by the user.

Sample Array Program

/* Simple C program to store the elements in the array and to print them from the array */
void main()
     int array[5],i;
     printf("Enter 5 numbers to store them in array \n");
     printf("Element in the array are - \n \n");
           printf("Element stored at a[%d] = %d \n",i,array[i]);

Input – Enter 5 elements in the array – 23   45   32   25   45
Output – Elements in the array are –
Element stored at a[0]-23
Element stored at a[0]-45
Element stored at a[0]-32
Element stored at a[0]-25
Element stored at a[0]-45

Programmers can now get an easy remote access to their programming and testing tools from anywhere on any device by loading it into cloud with desktop hosting service from Visit to know more about hosted SharePoint and other hosted software products for better team collaboration.

yashan has written 69 articles

12 thoughts on “One – Dimensional Arrays

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>