In C language, an array is a collection of elements of the same data type stored in contiguous memory locations. A one-dimensional array is the simplest type of array, where elements are stored in a single row.
Here is the syntax for declaring a one-dimensional array:
Code:
datatype arrayname[size];
datatype refers to the data type of the array elements (e.g. int, float, char, etc.).
arrayname is the name of the array.
size is the number of elements in the array.
Here is an example of declaring a one-dimensional array of integers with size 5:
To access individual elements of the array, you can use the index number. The index of the first element in an array is 0 and the last element's index is
size-1.
Here is an example of assigning a value to an element of the array:
You can also initialize the array at the time of declaration:
Code:
int numbers[5] = {10, 20, 30, 40, 50};
In this case, the first element of the array is initialized to 10, the second element to 20, and so on.
Note that arrays in C are indexed starting from 0 and, unlike some other programming languages, C does not perform any boundary checks. Therefore, it is important to ensure that the array index is within the valid range to avoid memory access violations or other errors.