What is the method of initializing values in C language arrays?

There are two ways to initialize and assign values to arrays in C language.

  1. Static initialization: assigning initial values to array elements directly when defining the array, enclosed in curly braces { }, for example:
int arr[5] = {1, 2, 3, 4, 5};
  1. Dynamic initialization: After defining an array, assigning values to the array elements through a loop or other means, for example:
int arr[5];
for(int i = 0; i < 5; i++) {
    arr[i] = i + 1;
}
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds