How to create an array in C language?
In C language, you can create arrays using the following syntax:
- Declare the type and name of the array, and specify the size of the array using square brackets [].
dataType arrayName[arraySize];
- For example, create an array containing 5 integers.
int numbers[5];
- It is possible to initialize an array when declaring it.
int numbers[5] = {1, 2, 3, 4, 5};
- You can also declare the array first, and then assign values to it one by one.
int numbers[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
- Array indexes start from 0, and you can access elements in the array using square brackets and the index value.
int x = numbers[0]; // 获取第一个元素
numbers[1] = 10; // 修改第二个元素
This is the basic way of creating an array in C language. Note that the size of the array must be determined at compile time and cannot be changed.