How do you assign values to an array in the C language?

There are multiple ways to assign values to an array in the C language.

  1. Assigning values one by one: You can use loop statements to assign values to array elements individually.
int arr[5];
int i;

for(i = 0; i < 5; i++) {
    arr[i] = i + 1;
}
  1. Using curly braces for initialization: Arrays can be initialized using curly braces, assigning values to array elements in order.
int arr[5] = {1, 2, 3, 4, 5};
  1. Assigning values with specified indexes: You can assign values to array elements using specified indexes.
int arr[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;

Please note that in C language, array indexes start from 0, so the index of the first element is 0, the second element is 1, and so on.

bannerAds