
In the array, the index starts at 0 for the first elements and then progresses one by one. The elements start filling from the left to the right until the size of the data items.
You can easily store and manipulate large amounts of data stored in a C array. You can store different data types, such as int, float, char, double, etc. You can also store user-defined data types such as pointers, structures, etc.
| 1 | 4 | 8 | 25 | 2 | 17 |
| 1 | 4 | 8 | 25 | 2 | 17 |
| Access Elements in an array |
| int arr[6] = {1, 4, 8, 25, 2, 17}; printf ("%d", arr[2]); // Accessing the third element at index 2, which is 8 |
| Array initialization using simplest approach |
| arr [0] = 1; // initialising array in C arr [1] = 4; arr [2] = 8; arr [3] = 25; arr [4] = 2; arr [5] = 17; |
| Array in C |
| int arr[6] = { 1, 4, 8, 25, 2, 17} |
| Array without size |
| int arr[ ] = { 1, 4, 8, 25, 2, 17} |
| Array using loops |
| for( int i=0 ; i<N; i++){ name_of_array[ i ] = value i;} |
| Array in C |
| #include <iostream> int main() { int elements[] = {1, 4, 8, 25, 2, 17}; int arrSize = sizeof (elements) / sizeof (elements[0]); // Initializing array elements using for loop for (int i = 0; i < arrSize; i++) { elements[i] = arr[i]; } cout << "Array elements after initialization using for loop:" <<endl; for (int i = 0; i < arraySize; ++i) { cout << elements[i] << " "; } cout <<endl; return 0; } |
| Sorting in Array |
| #include<stdio.h> void main () { int i, j,temp; int a[6] = {1, 4, 8, 25, 2, 17 }; for(i = 0; i<6; i++) { for(j = i+1; j<6; j++) { if(a[j] > a[i]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf ("Printing Sorted Element List ...\n"); for(i = 0; i<6; i++) { printf ("%d\n",arr[i]); } } |
| Update Elements in an Array |
| arr [ 0 ] = 9; |