C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
one dimensional array in cArray in c | One dimensional Array in c programming The variables that we used till now are capable to store only one value at a time. let us consider situation when we want to store 100 students name and student roll numbers, so it would be difficult to stores with previous data type its will be lengthy. Here the concept of Array useful to stores any type data of number of students or number of employees. The array is a collection of elements of similar Type or data type. The data type of elements any valid data type in c such as int, char or float so the every elements of array share the same variable name is called as Array name and each element has different index number know as subscript. Let us take a problem if we want to store age of 100 students, we can take a variable age of size 100 and of type int. The size of this array age is 100 so it can stored 100 integer values. let us example how it is stored in memory age[0],age[1],age[2],age[3],age[4],age[5],age[6],age[7].age[8],age[9],………….age[97],age[98],age[99] In c the index number starts from 0 , so age[0], is the first element, age[1] is the second element of array and last element of array is less then the size of array age[99]. Array can be Single dimensional or multidimensional, The number of indexes or subscripts determine the dimensional of the array.So the one dimensional array has one subscript or index, two dimensional has two subscripts and so on. The one dimensional array called as vector and two dimensional array called as matrix. When we use any variable, before use we declare the variable, like other variables, array should also declare before it sis used in program. The syntax of declaration of the Array is
in this example array_name is the name of array, It can be valid c identifier. The data_type is the data type of array (int, char, float etc) and the size of array indicates the number of elements can stored in the array, so here some examples of array are– .
Here salary is the array of type int which can store 50 elements of type int, The age array is also type of int which can stored 50 elements and in the last name array is the type of char which can stored two characters.
After the declaration. in the array the default values of elements are garbage value but while the global and static arrays are automatic initialized to zero. so we can also explicitly initialize array at the time of declaration. so the syntax of Initialize of array is: data_type array_name[size]={value1,value2,value3……….valueN}; Here in the example array_name is the name of array and size is the size of array indicates the how many elements can stored in array, and the value1,value2,value3 and valueN are the constant values are known as Initializers , which is assign to the array elements one by one, all the values seprated by commas and at the end of braces there is semi colon for example:
|