How to define array format in C language?
One common method in the C language for defining the format of an array using #define macros is to enclose the initialized values of the array elements in curly brackets {}. An example is shown below:
#define ARRAY_SIZE 5
#define ARRAY_FORMAT {1, 2, 3, 4, 5}
int main() {
int array[ARRAY_SIZE] = ARRAY_FORMAT;
// 输出数组的元素
for (int i = 0; i < ARRAY_SIZE; i++) {
printf("%d ", array[i]);
}
return 0;
}
In the example above, a macro called ARRAY_SIZE is defined to represent the size of an array. Additionally, another macro called ARRAY_FORMAT is defined to represent the format of the array. In the main function, the ARRAY_FORMAT macro is used to initialize the array using the statement int array[ARRAY_SIZE] = ARRAY_FORMAT. Finally, the elements of the array are outputted using a loop.
Please note that macro definitions only perform text replacement during the compilation phase, so it simply replaces ARRAY_FORMAT with {1, 2, 3, 4, 5}. Therefore, macro definitions can only be used for array initialization and cannot be used to define dynamically sized arrays.