Array in C
In programming Language there is variable
used to store data. This variable is one type of memory location, where we
store the data, but variable can store only one data at a time, So, if we want
to store more than one data at a time, then we can use array.
Arrays a kind of data structure that 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.
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 −
typearrayName [ arraySize ];
This is called
a single-dimensional array. The arraySize must be an
integer constant greater than zero and type can be any valid C data
type. For example, to declare a 10-element array called A of type float,
use this statement −
Float A[3];
Here A is a variable array which is
sufficient to hold up to 3float numbers.
How data store in Memory:
Array always stores data in memory position
wise or as per index. Index start always from 0.
A[0]
|
A[1]
|
A[2]
|
Intstudent[5];
Here, we store int data type data in array
named student. Data store for 5 student.
Initializing Arrays
We can assign values to the array, when
declare array, It is called Initializing Arrays.
i.e. datatypearray[size]={ values….};
int student[5]={ 12,45,67,23,90};
int student[]={12,56,98};
We can keep blank also size of array in
initialization, compiler can count its maximum size from initialization values.
Input Values for Array:
We have to use array in for loop because
array has more than one values.
For (i=0; i<=n; i++)
{
Scanf(“%d”,
&student[i]);
}
Here, loop executes n times and input values
n times, stores values into the given address.
Output Values for Array:
Same as input values as for output values;
For (i=0; i<=n; i++)
{
printf(“%d”,
student[i]);
}
Example of Array programme to enter data and
display data.
#include <stdio.h>
int main () {
int n[ 10 ]; /* n is an array of 10 integers
*/
inti,j;
/*
initialize elements of array n to 0 */
for ( i = 0; i< 10; i++ ) {
n[ i ] = i + 100; /* set element at location
i to i + 100 */
}
/*
output each array element's value */
for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %d\n", j,
n[j] );
}
return 0;
}
To Learn More about C language
course in Ahmedabad, C language course, Computer Course, Computer Class
Visit us@
https://tccicomputercoaching.wordpress.com/
Call us @ 9825618292
No comments:
Post a Comment