Introduction to Array in C | One dimensional array in C

Preview
You must first complete for statement in c language | Nesting of for in c language before viewing this Lesson

Introduction to Array in C

Array is a collection of homogeneous elements having same data type and same Size. All the elements of an array are stored in consecutive memory locations.

All the elements of an array are referenced by a single name.

Each individual element of any array can be processed using index of array elements. The index is always an integer value. Array index always starts from zero.

Arrays are basically used when we require large number of variables of same data type.

Types of Array

Arrays are of three types:

  • One dimensional Array
  • Two dimensional Array
  • Multi dimensional Array



One dimensional Array in C

Array having one dimension is known as one dimensional array. We use only one subscript to specify the size of array or to refer to any element of the array.

Before using an array in a program, array must be declared.

The syntax for declaring an array is:

<Type> Arr_name[Size];

Here, Data_Type refers to any valid data type of C language. It can be any primary data type like int, float, char etc or can be any secondary data type like structure, union and class.

Arr_name  specifies the name of array as defined by the programmer. The name of array must follow all the rules of a valid identifier of C language.

[ ] Square brackets are known as subscript, they are used to declare Size of array within them.

Size specifies the maximum number of values which can be stored in an array.

Example

we can declare an array as:

int A[6];

Array A can be represented in memory as follows:

Array named A contains 6 elements referred as A[0],A[1],A[2],A[3],A[4] and A[5]. Here 0,1,2,3,4 and 5 are known as indexes of array elements i.e. index of first element of array is 0, index of second array element is 1 and so on.

Array elements are stored in consecutive memory locations. So if first element of array i.e. A[0] is stored at memory location 1002, next array element must be stored at location 1004 and so on.

There is a gap of 2 between two consecutive addresses because Size of int data type is 2.

In case of float type array, this gap will be of 4 as float data type is of 4 bytes.

In case of char data type, this gap would be of 1 byte as char data type is of 1 byte.

Program to demonstrate the use of one dimensional array.
#include<stdio.h>
int main()
{
int a[3];
a[0]=10;
a[1]=5;
a[2]=20;
printf(“\na[0]=%d”,a[0]);
printf(“\na[1]=%d”,a[1]);
printf(“\na[2]=%d”,a[2]);
return(0);
}
Output
a[0]=10
a[1]=5
a[2]=20
Description

In the above program, we have declared an array a having Size 3.

This array can contain three values.

First element of array is named as a[0], second value is named as a[1] and third value is named as a[2].

Here, 0,1 and 2 represent the index of array elements. The largest index of array should be one less than the Size of array.



Initialization of a One dimensional Array in C

We can initialize an array just as we declare and initialize an ordinary variable. Syntax for initializing a one dimensional array is:

<Data_Type> Arr_name[Size]={List_of_values};

Data_Type refers to any valid data type of C language. It can be any primary data type like int, float, char or any secondary data type like structure or union.

Arr_name  specifies the name of array as defined by the programmer. The name of array must follow all the rules of a valid identifier of C language.

[ ] Square brackets are known as subscript, they are used to declare Size of array within them. While initializing an array, these brackets may be kept blank i.e. we may or may not specify any Size within them.

Size specifies the maximum number of values which can be stored in an array. It is optional while initializing an array. If we don’t specify any size, the compiler will automatically provide size to array depending upon the number of values within the braces.

List_of_values specifies the various values which are stored in an array. These values should be separated from each other with the help of commas.

 Example

int a[3]={5,9,10};  //Initialization of int type array

float b[4]={3.5,5.5,7.5,9.5}; //Initialization of float type array.

char c[8]={‘L’,’o’,’v’,’e’,’j’,’o’,’t’,’\0′}; //Initialization of char type/String array.

char d[8]=”Lovejot”; //Initialization of char type/String array.

int  e[]={3,5,7};  //Initialization of int type array without size specification.

Program to demonstrate the initialization of one dimensional array.
#include<stdio.h>
int main()
{
int a[3]={10,20,30};
int i;
clrscr();
printf(“\nArray elements are:=”);
for(i=0;i<3;i++)
printf(“\n%d”,a[i]);
return(0);
}
Output
Array elements are:=
10
20
30
Description
In the above program, we have initialized an array a having size 3. This array contains three values. First element of array i.e. a[0] contains 10, second element of array i.e. a[1] contains 20 and third element a[2] contains 30. We can also leave the size blank while initializing the array.



Program to read and display a  one dimensional array.
#include<stdio.h>
int main()
{
int a[3]={10,20,30};
int i;
printf(“\nEnter values in array elements :=”);
for(i=0;i<3;i++)
scanf(“%d”,&a[i]);
printf(“\nArray elements are:=”);
for(i=0;i<3;i++)
printf(“\n%d”,a[i]);
return(0);
}
Output
Enter values in array elements :=3
4
5
Array elements are:=
3
4
5
Description

In the above program, we have declared an array a having size 3.

I have used following statements to read the array.

for(i=0;i<3;i++)
scanf(“%d”,&a[i]);

First element of array i.e. a[0] gets 3, a[1] gets 4 a[2] gets 5.

Elements of array are displayed using following statements:

for(i=0;i<3;i++)
printf(“\n%d”,a[i]);



TIPS and tricks

  • An array holds elements that have the same data type
  • Array elements are stored in subsequent memory locations
  • Array name represents the address of the starting element
  • Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.
  • The amount of memory an array can consume depends on the data type of an array.
  • Character Array works as string .
  • Null array :- Array is declared but it’s size is not define as null array Example : int array[ ];
  • Empty array :- Array is declared but it has no elements is empty array Example: int a[5]={};
  • When we enter more values/elements than the size of an array, then elements exceeding size of array will be stored on next memory blocks in continuation due to which  program may unsuccessfully terminate, system may get halted because those extra memory blocks may contain any of operating system file , or any other useful file.



Test Your Knowledge

Write C program for following:

  1. Program to insert an element at the end of the array.
  2. Program to delete and element from an array.
  3. Program to reverse the array.
  4. Program to double the even elements and triple the odd elements of a one dimensional array.
  5. Program to copy even elements and odd elements of a one dimensional array separately to two different arrays.
  6. Program to find the smallest number from a two dimensional array.
  7. Program to interchange the alternate elements of a one dimensional array.

Write the output of the following programs:

1.

main( )
{
int a[]={65,66,67};
int i;
for(i=0;i<3;i++)
printf(“%c”,a[i]);
}

2.

main( )

{
static int arr[4] ;
int i ;
for ( i = 0 ; i < 4 ; i++ )
printf (“\n%d”, a[i] ) ;
}

3.

main( )

{
char a[]=”India”;
int i;
for(i=0;i<5;i++)
printf(“\n%c”a[i]);,
}

4.

main( )

{
int testList[5];
int sizeofList = sizeof(testList);
printf(“sizeof list = %d”, sizeofList);
}
5.
main()
{
char str [15] = “India2012”;
str[10] = ‘.’;
str[11] = ‘c’;
str[12] = ‘o’;
str[13] = ‘m’;
str[14] = ‘\0’;
puts(str);
}
6.
main()
{
char str [ ] = { ‘G’ , ‘y’ , ‘a’, ‘n’, ‘t’, ‘o’, ‘n’, ‘i’ , ‘c’, ‘\0’};
printf(“%d”, strlen(str) );
}

Best Books of C





Lesson tags: initialization of one dimensional array in c, one dimensional array in c, reading one dimensional array in c, types of array in c
Back to: C Programming Language