0
0
Cprogramming~10 mins

Array initialization in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array initialization
Declare array variable
Specify size or initializer
Assign values to elements
Array ready to use
This flow shows how an array is declared, given a size or values, and then ready for use.
Execution Sample
C
int arr[3] = {10, 20, 30};
// Initialize array with 3 elements

int arr2[5] = {1, 2};
// Remaining elements set to 0
This code creates arrays with initial values; missing values default to zero.
Execution Table
StepActionArray StateExplanation
1Declare int arr[3][?, ?, ?]Array of size 3 created, values undefined
2Initialize arr with {10, 20, 30}[10, 20, 30]All elements assigned explicitly
3Declare int arr2[5][?, ?, ?, ?, ?]Array of size 5 created, values undefined
4Initialize arr2 with {1, 2}[1, 2, 0, 0, 0]First two elements assigned, rest zeroed
5End[10, 20, 30] and [1, 2, 0, 0, 0]Arrays ready for use
💡 Initialization complete; arrays contain assigned or default values
Variable Tracker
VariableStartAfter Step 2After Step 4Final
arrundefined[10, 20, 30][10, 20, 30][10, 20, 30]
arr2undefinedundefined[1, 2, 0, 0, 0][1, 2, 0, 0, 0]
Key Moments - 3 Insights
Why do some elements become zero when not all are initialized?
In step 4, arr2 is initialized with fewer values than its size. The remaining elements are automatically set to zero by the compiler, as shown in the execution_table row 4.
What happens if I declare an array without initializing it?
In steps 1 and 3, arrays are declared but not initialized, so their elements contain garbage (undefined) values until assigned.
Can I initialize an array without specifying its size?
Yes, but not shown here. If you omit size and provide values, the compiler sets size to the number of values. This example uses explicit sizes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of arr[1] after step 2?
A20
B10
C30
Dundefined
💡 Hint
Check execution_table row 2 under Array State
At which step does arr2 get its remaining elements set to zero?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table row 4 explanation
If arr2 was declared as int arr2[3] = {1, 2}; how would the final array look?
A[1, 2, 3]
B[1, 2, 0]
C[1, 2, undefined]
D[0, 0, 0]
💡 Hint
Refer to variable_tracker for arr2 initialization pattern
Concept Snapshot
Array initialization in C:
- Declare with type and size: int arr[3];
- Initialize with values: int arr[3] = {10, 20, 30};
- Missing values default to 0
- Uninitialized arrays contain garbage
- Arrays ready for use after initialization
Full Transcript
This lesson shows how arrays are created and initialized in C. First, an array is declared with a type and size. Then, values can be assigned using curly braces. If fewer values are given than the size, the rest become zero. If no values are given, the array contains undefined values. This is important to know so you don't use garbage data. The example shows two arrays: one fully initialized and one partially initialized with zeros filling the rest.