Wednesday, February 11, 2015

Arrays in C Part 1

In the last two tutorials I told you about the preprocessor directives. Today I will start giving you some advance lessons in C programming. And these tutorials starts from Arrays. So in this tutorial I will tell you about the arrays in C and its use. Lets start it with one basic question.


What are Arrays in C?

An array is a collection of similar type of data. Remember here similar means it belongs to one data type. Array is generally used to store many values which belong to one data type.


Why Array is used?

Till now we have some very basic programs. Suppose when we have to make a program to store the marks details of one student. Then we can easily make it by using simple variables. But when we asked to store details of 100 students then it will be very tedious task. We have to take a lot of variables to store those values. And that will be not an efficient way.

In those cases array is used quite easily. It provides an opportunity to store all the values in single array with single name. So we can easily call and receive data with single name.


Declaration of Array

int x[10];

In our above code int is the data type of an array. “x” is the variable name (array name in our case). “[10]” is something new. Well it defines the size of array. “10” means that this array will store 10 integer values in it. Square brackets shows that it is an array. We can create array of any other data type also. Some more examples are given below.

Examples

char a[5];
float b[15];


Initialization of Array

We have declared an array. Now its time to initialize it or storing some values in it. We can do it at the time of its declaration.

int x[5]={1,5,3,5,6};

or

int x[]={5,6,3,5,5};

In the first statement I have initialized the array with five values. And in the second statement I have initialized it without defining the size of an array. Remember when we declare and initialize the array at the same time then mentioning the size of array is optional.


Accessing Array Elements

Subscript is used to access the specific position element inside an array. Subscript is also known as index.

Remember that a subscript or index always starts with 0 end at n-1, here n is the maximum size of the array. If there is an array of size 10 then its subscript will be from 0 to 9.

For example if we want to access fourth element of the array x that we have declared above then it can be done in following way.

int a=x[3];

See here for accessing fourth element we are using index 3 because the subscript or index starts from 0. In the same way for accessing fifth element we will use index 4 i.e. x[4].


Printing Data of Array

Now it’s a perfect time to build our first program which will print all the values of an array.

#include<stdio.h>

void main()
{
int x[5]={33,66,43,63,67};
int i;
for(i=0;i<5;i++)
{
printf("%d
",x[i]);
}

}


Output

Arrays in C - Part 1

Explanation

The program is almost self explanatory. But note down the few points.
  • I have declared an integer array x at the beginning and I have initialized it with some values.
  • Now I have used a for loop to print the values inside it.
  • Remember, subscript of array always starts with 0. So I have initialized variable i with 0.
  • Now I have used x[i] in printf() function to print each value on the screen.