Modal Box

Array in C Language

When we work with large number of data values, we also need that many number of different variables. As the number of variables increases, the complexity of the program also increases and so the programmers get confused with the variable names. There may be situations where we need to work with large number of similar data values. To make this work more easy, C programming language provides a concept called “Array”.

An array is a special type of variable used to store multiple values of same data type at a time.

Types of Array :
  1. single dimensional array.
  2. multi dimensional array ( discussed in the next section.)

How to declare an array

The syntax to declare an array is <datatype> <array_name> [size];
eg : int a[10];
Here, ‘a’ is an array name of  size 10 and it can store integer type values.
The index (position) of the array element starts from 0 and last index is [arraysize] – 1 ( 9 -> last index in this example).

  Suppose, we have to store a value at the given index (either from 0 to the last index)
[index]=value;
eg: a[2]=10 ;
it means that at 2nd index /third location of the array, 10 is stored.

Similarly, we can take input and print the values of array as following:
for input: scanf(“%d”,&a[0]);   // scans the integer value at first index of the array.
for display: printf(“%d”,a[0]);

Program to take input 10 elements in an array and print them back in reverse order.

#include<stdio.h>
#include<conio.h> 
void main()
{
   int a[10], i=0;
   printf("enter ten array elements");
       // take input in the array.
   while(i<10)
     {
         scanf("%d", &a[i]);
         i++;
      }
   i=9;  // last index of the array because we have to print it in reverse order.
   while(i>=0)
     {
         printf("%d ", a[i]);
         i-- ;
      }
}

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial