How to assign values to an integer array in C language?
There are multiple ways to assign values to an int array, here are two common methods:
- Assign values one by one using a loop.
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i + 1; // 为数组元素赋值
}
- Use static initialization:
int arr[] = {1, 2, 3, 4, 5}; // 直接在定义数组时赋值
Both of the methods can be used to assign values to an integer array, depending on the specific situation.