An **Array** is a fixed-size, sequenced collection of elements of the same data type. It is one of the most important data structures in C because it allows you to store multiple values in a single variable name, making data management much more efficient.
Imagine you need to store the marks of 5 students. Without arrays, you would need to create five separate variables: mark1, mark2, mark3, mark4, mark5. With an array, you can simply create one variable: int marks[5];. This makes it easier to perform operations like sorting, searching, or calculating averages using loops.
int or all float).To declare an array, you specify the data type, the name of the array, and the number of elements it will hold inside square brackets [].
Array elements are accessed using an **Index Number**. In C, array indexing always starts at 0. This means the first element is at index 0, the second is at index 1, and the last element is at index size - 1.
The most efficient way to work with arrays is by using a for loop. Since we know the size of the array, we can use the loop counter as the index to access each element.
C also supports multidimensional arrays. The most common is the **2D Array**, which can be thought of as a table with rows and columns. This is particularly useful for representing matrices or game boards.
| Concept | Description |
|---|---|
| Memory Calculation | Total size = Number of elements × Size of one element. |
| Array Bounds | C does not check if an index is out of bounds (Runtime crash risk). |
| Base Address | The name of the array points to the address of the 0th element. |
myNumbers[5] for an array of size 5 will lead to "Undefined Behavior" because valid indices are only 0 to 4.int arr[5]; and printing it without assigning values will result in "garbage" (random) numbers.sizeof operator: int length = sizeof(arr) / sizeof(arr[0]);. This makes your loops safer and more flexible!