Wednesday

1-Dimensional Array in C Programming Language


 1-Dimensional Array in C Programming Language

An array is a collective name given to a group of similar variables.
An array can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on. In this topic, we will discuss 1-Dimensional arrays in C Programming Language. So lets start with 1-Dimensional array.

Let us understand this with the help of an example.


void main(){
     int arr[3],i;
     printf(“Enter 3 values\n”);
     for(i=0;i<3;i++){
          scanf(“%d”,&arr[i])
     }
     printf(“The entered values are:\n”);
     for(i=0;i<10;i++){
          printf(“%d\t”,arr[i])
     }
}

Explanation:

int arr[3] statement declares an array capable of holding 3 integer values. The first value can be accessed using arr[0]. Similarly second value can be accessed using arr[1]. Note that the number enclosed in [] is called index value. The index value of array in C always starts from 0.
So,
     First element --> arr[0]
     Second element --> arr[1]
     Third element --> arr[2]
     nth element --> arr[n-1]

When an array is declared, a garbage value is stored in it. Thus, accessing the value of array elements without defining it will give garbage value.

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

The above for loop accepts the value from the user and stores it in an array. The array that was declared before is getting defined here.

Thus,
     First value corresponding to 1st iteration of for loop is stored in the element arr[0]
     Second value corresponding to 2nd iteration of for loop is stored in the element arr[1]
     Third value corresponding to 3rd iteration of for loop is stored in the element arr[2]

for(i=0;i<3;i++){
     printf(“%d\t”,arr[i])
}
The above for loop displays the value stored in the array.
Thus,
     1st iteration displays value stored in arr[0]
     2nd iteration displays value stored in arr[1]
     3rd iteration displays value stored in arr[2]

In the above program, we have used data type int for the array. Hence it is called integer array. We can also declare array as float. But then it will contain float values. An array element can only hold one type of data i.e. either integer or float or character.

Consider this program,
void main(){
     int arr[3];
     arr[0] = 1;
     arr[1] = 2;
     arr[2] = 3;
}
Let us suppose that base address of this array is 6000. So value 1 is stored at memory location 6000. Since it is integer array, 16 bit C compiler will assign 2 bytes of memory for its storage. Hence next value i.e. 2 will be stored at location 6002. Similarly, 3 will be stored at location 6004.

An array can be defined as follows:
int arr[3]={1,2,3};
i.e., arr[0] --> 1
       arr[1] --> 2
       arr[2] --> 3

An array can also be defined as:
int arr[]={1,2,3}

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |