
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Initialization of variable sized arrays in C
Variable sized arrays are data structures whose length is determined at runtime rather than compile time. These arrays are useful in simplifying numerical algorithm programming. The C99 is a C programming standard that allows variable sized arrays.
A program that demonstrates variable sized arrays in C is given as follows −
Example
#include int main(){ int n; printf("Enter the size of the array:
"); scanf("%d", &n); int arr[n]; for(int i=0; i<n; i++) arr[i] = i+1; printf("The array elements are: "); for(int i=0; i<n; i++) printf("%d ", arr[i]); return 0; }
Output
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 us understand the above program.
The array arr[ ] is a variable sized array in the above program as its length is determined at run time by the value provided by the user. The code snippet that shows this is as follows:
int n; printf("Enter the size of the array:
"); scanf("%d", &n); int arr[n];
The array elements are initialized using a for loop and then these elements are displayed. The code snippet that shows this is as follows −
for(int i=0; i<n; i++) arr[i] = i+1; printf("The array elements are: "); for(int i=0; i<n; i++) printf("%d ", arr[i]);
- Related Articles
- Variable initialization in C++
- Initialization of a multidimensional arrays in C/C++
- Initialization of a multidimensional arrays in C/C++ Program
- Explain the variable declaration, initialization and assignment in C language
- Variable Length Arrays in C and C++
- Initialization of local variable in a conditional block in Java
- Does C++ support Variable Length Arrays
- C program to demonstrate usage of variable-length arrays
- Initialization of static variables in C
- Zero Initialization in C++
- Uniform Initialization in C++
- Collection Initialization in C#
- Why Java wouldn't allow initialization of static final variable in a constructor?
- Initialization of a multidimensional array in C
- Initialization vs Instantiation in C#

Advertisements