English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Initialize a multidimensional array in C

Variable-size arrays are data structures whose length is determined at runtime rather than compile time. These arrays are very useful in simplifying numerical algorithm programming. C99It is a C programming standard that allows the use of variable-size arrays.

The program demonstrating variable-size arrays in C language is as follows shown-

Example

#include
int main(){
   int n;
   printf("Enter the size of the array: \n");
   scanf("%d", &n);
   int arr[n];
   for(int \t i=0; \t i<n; \t i++)
   arr[i] \t=\t i+1;
   printf("The array elements are: \t");
   for(int \t i=0; \t i<n; \t i++)
   printf("%d \t", arr[i]);
   return 0;
}

Output Result

The output of the above program is as follows-

Enter the size of the array: 10
The array elements are: 1 2 3 4 5 6 7 8 9 10

Now let's understand the above program.

The array arr [] is a variable-size array in the above program, because its length is determined by the value provided by the user at runtime. The code snippet is as follows shown:

int n;
printf("Enter the size of the array: \n");
scanf("%d", &n);
int arr[n];

Use a for loop to initialize array elements and then display these elements. The code snippet is as follows-

for(int \t i=0; \t i<n; \t i++)
arr[i] \t=\t i+1;
printf("The array elements are: \t");
for(int \t i=0; \t i<n; \t i++)
printf("%d \t", arr[i]);