Declaration
Tags: #◇CCPROG2 #◇C Programming #◇Programming #◇Computer Science #◇Data Structures #◇Data Types #◇Structured Programming #◇Memory Management
◇Overview
Array declaration in C specifies the data type, name, and size of the array, allocating a contiguous block of memory.
◇Key Concepts
Syntax: dataType arrayName[arraySize]; Size must be a compile-time constant (in standard C89) or variable-length (C99+) Memory is allocated contiguously Elements are uninitialized by default (contain garbage values)
◇Details / Theory
Syntax
dataType arrayName[arraySize];
dataType — the type of each element (int, float, char, etc.) arrayName — identifier for the array (follows C naming rules) arraySize — positive integer specifying number of elements
Examples
int numbers[10]; // Array of 10 integers
float prices[5]; // Array of 5 floats
char name[30]; // Array of 30 characters (for strings)
Memory Allocation
Declaration allocates arraySize × sizeof(dataType) bytes contiguously:
int arr[10]; // Allocates 10 × 4 = 40 bytes (assuming 4-byte int)
Variable-Length Arrays (VLA) — C99+
int n;
scanf("%d", &n);
int arr[n]; // Size determined at runtime (C99 feature)
[!tip] Key Insight
VLAs are allocated on the stack and cannot be initialized at declaration.
Multi-Dimensional Declaration
int matrix[3][4]; // 2D array: 3 rows, 4 columns
int cube[2][3][4]; // 3D array
◇Common Pitfalls
Using a variable as array size in C89 (only allowed in C99+). Forgetting that uninitialized arrays contain garbage values, not zeros. Declaring arrays with size 0 — this is not valid in standard C.
◇Summary
TL;DR — Array declaration in C uses
dataType name[size];to allocate contiguous memory. Size must be a positive constant (C89) or variable (C99+). Uninitialized elements contain garbage values.
◇Links
Related: ◇Initialization — ◇Size — ◇Element Type — ◇Memory Allocation — ◇One-Dimensional Arrays (1D Arrays) Source / Reference:
◇References & Navigation
- Previous Topic: ◇Size
- Next Topic: ◇Memory Allocation
- Related Notes: ◇Initialization, ◇Memory Allocation
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
Which of the following is required when declaring a fixed-size array?
What happens if you declare an array without initializing it (e.g., int arr[5];) inside a function?
Which declaration is syntactically invalid in standard C89?