What is the method of directly assigning values to arrays in C language?

In C language, you can use braces to directly assign a group of elements to an array. This method is called array initialization.

For example, the following code initializes an integer array arr with 3 elements containing the values 1, 2, and 3.

Create an array named “arr” with three elements: 1, 2, and 3.

Similarly, the following code initializes the character array str as an array containing 4 elements, which are ‘h’, ‘e’, ‘l’, and ‘l’:

The variable “str” is an array of characters containing the letters “h”, “e”, “l”, “l”.

It is important to note that the size of the array must match the number of initialized elements, otherwise it will result in a compilation error. If the number of initialized elements is less than the size of the array, the remaining elements will be automatically initialized to 0 (for numerical types) or ‘\0’ (for character types). If the number of initialized elements is greater than the size of the array, it will result in a compilation error.

bannerAds