Tuesday 28 January 2014

Array:


C programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Array is a collection of elements that has same data type and identify by a single name.


There are 3 types of array in C programming language.
A) Single dimensional array.
B) Two dimensional array.
C) Multi dimensional array.

Declaring Arrays:
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows:

Type arrayName [arraySize];

Int x[20];
Char ff[30];




Initialization of one-dimensional array:
Arrays can be initialized at declaration time in this source code as:
char arr[5]={“abcd”};
char arr[5]={‘a’,’b’,’c’,’d’};
char arr[]={“abcd”};

* If at the declaration time we initialized any array the size of the array is not mandatory to declare.

Example:

#include<stdio.h>
void main()
{
    int i;
    int arr[5] = {10,20,30,40,50};  
    // declaring and Initializing array in C
    /* Above array can be initialized as below     also 
       arr[0] = 10;
       arr[1] = 20;
       arr[2] = 30;
       arr[3] = 40;
       arr[4] = 50; 
    */
    for (i=0;i<5;i++)
    {
        // Accessing each variable
        printf("value of arr[%d] is %d \n", i, arr[i]);
    }

}



OR...

#include<stdio.h>
void main()
{
char arr[20];
printf("Enter your name :\n");
scanf("%s",&arr);
printf("Your name is : ");
printf("%s\n",arr);
}



No comments:

Post a Comment

Thanks For Giving Comment In Our Blog.