Array Assignment in C
Array Assignment in C
In C, array assignment is done using the equal sign (=) operator. For example, if we have an array
called numbers with 5 elements, we can assign values to each element as follows:
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
This will assign the values 10, 20, 30, 40, and 50 to the first, second, third, fourth, and fifth elements
of the array, respectively.
It is important to note that array indices in C start from 0, so the first element of an array will always
have an index of 0. Also, when assigning values to an array, the number of elements on the right-
hand side of the equal sign must match the size of the array.
Arrays can also be assigned values during initialization. This can be done by enclosing the values in
curly braces and separating them with commas. For example:
This will create an array callednumbers with 5 elements and assign the values 10, 20, 30, 40, and
50 to its elements.
Another important aspect of array assignment in C is the use of loops. Loops allow us to assign
values to array elements in a more efficient and dynamic way. For example, we can use afor loop to
assign values to an array of size n as follows:
int numbers[n];
for (int i = 0; i < n; i++) {
numbers[i] = i + 1;
}
This will assign the values 1, 2, 3, ..., n to the elements of the array numbers.