- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Variable Length Arrays
![]() Share with a Friend |
C Programming - C Variable Length Arrays
Variable Length Arrays (VLA) in C
In C, Variable Length Arrays (VLAs) are arrays where the size is not determined at compile time but rather at runtime. This means the array's size is determined dynamically based on a value calculated during the program's execution. VLAs are allowed in C99 and later versions of the C standard.
Key Points:
- Definition: A Variable Length Array is an array whose size is determined at runtime, based on a variable or an expression.
- Declaring VLAs: They are declared just like normal arrays but the size is determined by a variable or a runtime expression.
- Scope: VLAs can only be declared inside functions (i.e., local scope).
- Memory: Memory for VLAs is allocated on the stack, and once the function exits, the memory is automatically freed.
- C99 Standard: VLAs were introduced in C99, but they are optional in C11 and later. Some compilers may not support VLAs if the compiler is not compliant with C99 or higher standards.
Syntax:
C
data_type array_name[size];
Where size is a variable or an expression whose value is determined at runtime.
Example:
C
#include <stdio.h>
void exampleFunction(int n) {
// Declare a VLA based on the runtime value of 'n'
int arr[n];
// Initialize the array
for (int i = 0; i < n; i++) {
arr[i] = i * i; // Store squares of i
}
// Print the array elements
printf("Array elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int size;
// Ask user for the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Call the function with the specified size
exampleFunction(size);
return 0;
}
Explanation:
- In the exampleFunction(), the size of the array arr is determined by the input parameter n, which is specified at runtime.
- The size of the array is determined dynamically, and memory for it is allocated on the stack.
- After performing operations on the array, the function exits, and the memory is automatically freed.
Output Example:
Enter the size of the array: 5
Array elements: 0 1 4 9 16
Advantages of VLAs:
- Dynamic Size: You can create arrays with sizes determined at runtime without using dynamic memory allocation functions (like malloc() or calloc).
- Automatic Memory Management: Since VLAs are allocated on the stack, they do not require explicit memory management, such as free().
- Simpler Syntax: VLAs offer the convenience of simpler syntax compared to manually allocating and deallocating memory using pointers.
Disadvantages of VLAs:
- Stack Limitations: Since VLAs are allocated on the stack, they are limited by the size of the stack. For very large arrays, using VLAs can lead to stack overflow errors.
- Limited Compatibility (C11 onwards): While VLAs are part of C99, they are optional in C11 and later versions of the C standard. Some modern compilers may not support VLAs, or they may issue warnings when VLAs are used.
VLAs vs Dynamic Memory Allocation:
- Variable Length Arrays: Automatically allocated on the stack. The size is known only at runtime, and they are easier to use since you don't need explicit memory management (like malloc()/free()).
- Dynamic Memory Allocation (malloc()/calloc()): Allocated on the heap, and you need to manually manage the memory with malloc() (allocation) and free() (deallocation). Heap memory allows for much larger arrays but requires explicit memory management.
C11 and Later Versions:
In C11, VLAs are considered optional, meaning they are not required to be supported by compilers. Therefore, if you're targeting C11 or later, and want portability, it's generally better to use dynamic memory allocation (malloc(), calloc(), etc.), especially for large arrays.
However, if you are working with C99 or older compilers that support VLAs, they can be very useful in many cases where array size is determined at runtime.
Summary:
- Variable Length Arrays allow array sizes to be determined at runtime.
- They are declared like regular arrays but with a size that is a variable or expression evaluated during execution.
- VLAs are allocated on the stack and automatically deallocated when the function exits.
- In C11 and later versions, VLAs are optional, and some compilers may not support them. It is important to check compatibility if targeting C11+ standards.
