Single (or) One dimensional arrays in C-language - C-Tutorial

Latest

Sunday, 9 October 2016

Single (or) One dimensional arrays in C-language

Single (or) One dimensional arrays


If an array followed by only one subscript, those arrays are called one dimensional arrays.
The general format of a one dimensional array is:
           
Declaration of an one dimensional array:  
           
Syntax:           datatype ArrayName[size];

Where,
  • datatype specifies the type of the elements will be stored in the array.
  • ArrayName specifies the name of the array. That arrayname should follow the valid identifier rules.
  • size indicates the maximum number of elements that can be stored inside the array.

Example:        int a[6];




INITIALIZATION OF ONE DIMENSIONAL ARRAYS

One dimensional array variables can be initialized into two different ways. Those are
1. Direct initialization
2. Through program execution (scanf() function)

Direct initialization:  We can initialize the elements of the array in the same way as the ordinary variables when they are declared.  The general form of initializing the one dimensional array is:
Syntax:           datatype Arrayname[size] = {List of Values};

            Here, List of Values is separated by comma operator.
                        Example:        int a[4] = {10,20,30,40};                    /* 10 20 30 40 */
                       

Through program execution:

/* EXAMPLE PROGRAM TO READ A ONE DIMENSIONAL ARRAY AND PRINT IT */

main()
{
            int n,i,a[10];
            clrscr();
            printf("\nEnter the no.of elements should be stored in array:");
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                            scanf("%d",&a[i]);
            }
            printf("\nThe given array elements are:");
            for(i=0;i<n;i++)
            {
            printf("%5d",a[i]);
            }
            getch();

}

No comments:

Post a Comment