Bird
Raised Fist0
Cprogramming~10 mins

Array initialization in C - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following correctly declares and initializes an integer array of size 3 with values 1, 2, and 3 in C?
easy
A. int arr = {1, 2, 3};
B. int arr[3] = {1, 2, 3};
C. int arr[3] = (1, 2, 3);
D. int arr[] = 1, 2, 3;

Solution

  1. Step 1: Understand array declaration syntax

    In C, arrays are declared with a type, name, and size in square brackets, e.g., int arr[3].
  2. Step 2: Check initialization format

    Initialization uses curly braces with comma-separated values, e.g., {1, 2, 3}.
  3. Final Answer:

    int arr[3] = {1, 2, 3}; -> Option B
  4. Quick Check:

    Correct array declaration and initialization [OK]
Hint: Use curly braces {} with size in brackets [] for arrays [OK]
Common Mistakes:
  • Missing square brackets for size
  • Using parentheses instead of braces
  • Assigning array without braces
2. Which of the following is a syntax error when initializing an array in C?
easy
A. int nums[4] = {10, 20, 30, 40};
B. int nums[5] = {0};
C. int nums[] = {5, 6, 7};
D. int nums[3] = {1, 2, 3, 4};

Solution

  1. Step 1: Check array size and initializer count

    Array size is 3 but initializer has 4 values, which is invalid in C.
  2. Step 2: Verify other options

    int nums[4] = {10, 20, 30, 40}; matches size and values, C infers size from initializer, B zero-initializes first element and rest are zero.
  3. Final Answer:

    int nums[3] = {1, 2, 3, 4}; -> Option D
  4. Quick Check:

    Initializer count > array size causes syntax error [OK]
Hint: Initializer list cannot exceed declared array size [OK]
Common Mistakes:
  • Providing more initial values than array size
  • Omitting size but giving initializer with extra values
  • Using wrong braces
3. What is the output of the following C code?
int arr[5] = {1, 2};
printf("%d %d %d %d %d", arr[0], arr[1], arr[2], arr[3], arr[4]);
medium
A. 1 2 0 0 0
B. 1 2 garbage garbage garbage
C. 1 2 3 4 5
D. Compilation error

Solution

  1. Step 1: Understand partial initialization

    When fewer values are given, remaining elements are set to zero automatically.
  2. Step 2: Check printed values

    arr[0] = 1, arr[1] = 2, arr[2], arr[3], arr[4] = 0 each.
  3. Final Answer:

    1 2 0 0 0 -> Option A
  4. Quick Check:

    Partial init fills rest with zero [OK]
Hint: Uninitialized array elements default to zero if partially initialized [OK]
Common Mistakes:
  • Assuming uninitialized elements have garbage values
  • Expecting compilation error for partial init
  • Confusing array size with initializer count
4. Identify the error in the following C code snippet:
int data[3];
data = {1, 2, 3};
medium
A. Missing semicolon after array declaration.
B. Array size must be omitted when initializing.
C. Arrays cannot be assigned values after declaration like this.
D. Braces should be parentheses for assignment.

Solution

  1. Step 1: Understand array assignment rules

    In C, arrays cannot be assigned values after declaration using = and braces.
  2. Step 2: Check other options

    Semicolon is present, size can be specified, braces are correct for initialization but not assignment.
  3. Final Answer:

    Arrays cannot be assigned values after declaration like this. -> Option C
  4. Quick Check:

    Array assignment after declaration is invalid [OK]
Hint: Arrays must be initialized at declaration, not assigned later [OK]
Common Mistakes:
  • Trying to assign array with braces after declaration
  • Confusing array assignment with pointer assignment
  • Using parentheses instead of braces
5. You want to create an integer array of size 6 where the first two elements are 10 and 20, and the rest are zeros. Which of the following is the best way to initialize it in C?
hard
A. int arr[6] = {0}; arr[0] = 10; arr[1] = 20;
B. int arr[6] = {10, 20, 0, 0, 0, 0};
C. int arr[6] = {10, 20};
D. int arr[6]; arr[0] = 10; arr[1] = 20;

Solution

  1. Step 1: Analyze partial initialization

    int arr[6] = {10, 20}; partially initializes first two elements; rest are zero by default, so it works.
  2. Step 2: Compare with explicit zero initialization and assignment

    int arr[6] = {0}; arr[0] = 10; arr[1] = 20; explicitly zeroes all elements then assigns first two, ensuring clarity and no garbage.
  3. Step 3: Evaluate best practice

    int arr[6] = {0}; arr[0] = 10; arr[1] = 20; is best for clarity and safety; int arr[6] = {10, 20}; works but less explicit; int arr[6] = {10, 20, 0, 0, 0, 0}; is verbose; int arr[6]; arr[0] = 10; arr[1] = 20; leaves uninitialized elements.
  4. Final Answer:

    int arr[6] = {0}; arr[0] = 10; arr[1] = 20; -> Option A
  5. Quick Check:

    Explicit zero init then assign first elements [OK]
Hint: Zero initialize first, then assign specific elements for clarity [OK]
Common Mistakes:
  • Assuming partial init always safe without explicit zero
  • Not initializing array before assignment
  • Overwriting zeros with uninitialized values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard