📖 Scenario: You are working on a simple program that stores the ages of five friends. You want to keep these ages in an array so you can use them later for calculations or display.
🎯 Goal: Create an array to hold the ages of five friends, set a variable for the number of friends, then print all the ages in order.
📋 What You'll Learn
Declare an integer array called ages with exactly 5 elements
Initialize the ages array with the values 21, 25, 19, 30, and 22 in that order
Create an integer variable called num_friends and set it to 5
Use a for loop with the variable i to iterate from 0 to num_friends - 1
Print each age followed by a space on the same line
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of data like scores, ages, or measurements in many programs.
💼 Career
Understanding arrays is essential for programming jobs because they are a basic way to organize and access multiple values efficiently.
Progress0 / 4 steps
1
Declare and initialize the array
Declare an integer array called ages with 5 elements and initialize it with these exact values: 21, 25, 19, 30, 22.
DSA C
Hint
Use the syntax int arrayName[size] = {values}; to declare and initialize an array in C.
2
Set the number of friends
Create an integer variable called num_friends and set it to 5.
DSA C
Hint
Use int num_friends = 5; to create and set the variable.
3
Loop through the array
Use a for loop with the variable i to iterate from 0 to num_friends - 1.
DSA C
Hint
Use for (int i = 0; i < num_friends; i++) to loop through the array indices.
4
Print the ages
Inside the for loop, print each age from the ages array followed by a space. After the loop, print a newline character.
DSA C
Hint
Use printf("%d ", ages[i]); inside the loop and printf("\n"); after the loop.
Practice
(1/5)
1. Which of the following is the correct way to declare an integer array of size 5 in C?
easy
A. array int arr[5];
B. int arr;
C. int arr = 5;
D. int arr[5];
Solution
Step 1: Understand array declaration syntax in C
In C, to declare an array of integers with size 5, the syntax is int arr[5];.
Step 2: Check each option
int arr[5]; matches the correct syntax. Options A, B, and D are incorrect because they either miss the size, use wrong keywords, or assign a value instead of declaring an array.
Final Answer:
int arr[5]; -> Option D
Quick Check:
Array declaration = int arr[5]; [OK]
Hint: Array size goes inside square brackets after type [OK]
Common Mistakes:
Forgetting to specify size inside brackets
Using wrong keywords like 'array'
Assigning a value instead of declaring
2. Which of the following is the correct way to initialize an integer array with values 1, 2, 3 in C?
easy
A. int arr[3] = (1, 2, 3);
B. int arr = {1, 2, 3};
C. int arr[3] = {1, 2, 3};
D. int arr[3] = [1, 2, 3];
Solution
Step 1: Recall array initialization syntax
In C, arrays are initialized using curly braces with values inside, and size must be specified if not inferred.
Step 2: Evaluate each option
int arr[3] = {1, 2, 3}; correctly declares an array of size 3 and initializes it with values 1, 2, 3. int arr = {1, 2, 3}; misses the size, C uses parentheses instead of braces, and D uses square brackets which is invalid.
Final Answer:
int arr[3] = {1, 2, 3}; -> Option C
Quick Check:
Array init uses braces and size = int arr[3] = {1, 2, 3}; [OK]
Hint: Use curly braces {} to initialize arrays with size [OK]
Common Mistakes:
Using parentheses () instead of braces {}
Omitting array size when required
Using square brackets [] for initialization
3. What will be the output of the following C code?
int arr[4] = {10, 20};
for(int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
medium
A. 10 20 0 0
B. 10 20 0 20
C. 10 20 10 20
D. 10 20 garbage garbage
Solution
Step 1: Understand partial initialization of arrays
When an array is partially initialized in C, the remaining elements are set to zero automatically.
Step 2: Trace the loop output
arr[0] = 10, arr[1] = 20, arr[2] = 0, arr[3] = 0. The loop prints all four elements separated by spaces.
Final Answer:
10 20 0 0 -> Option A
Quick Check:
Partial init fills rest with 0 = 10 20 0 0 [OK]
Hint: Uninitialized array elements default to 0 if partially initialized [OK]
Common Mistakes:
Assuming uninitialized elements have garbage values
Confusing partial initialization with full initialization
Miscounting array size in loop
4. Identify the error in the following C code snippet:
int arr[3];
arr = {1, 2, 3};
medium
A. Array size is not specified
B. Cannot assign values to array after declaration using braces
C. Missing semicolon after array declaration
D. Array elements must be initialized with parentheses
Solution
Step 1: Understand array assignment rules in C
In C, arrays cannot be assigned values using braces after declaration. Initialization must happen at declaration.
Step 2: Check the code snippet
The code tries to assign values to arr using braces after declaration, which is invalid syntax and causes a compilation error.
Final Answer:
Cannot assign values to array after declaration using braces -> Option B
Quick Check:
Array assignment after declaration with {} is invalid [OK]
Hint: Initialize arrays only at declaration with braces [OK]
Common Mistakes:
Trying to assign arrays after declaration with {}
Confusing initialization with assignment
Using parentheses instead of braces
5. Given the following code, what will be the output?
int arr[] = {5, 10, 15};
int *ptr = arr;
for(int i = 0; i < 3; i++) {
printf("%d ", *(ptr + i));
}
Choose the correct output.
hard
A. 5 10 15
B. 5 0 15
C. Garbage values
D. Compilation error
Solution
Step 1: Understand pointer and array relationship
In C, the name of the array is a pointer to its first element. Pointer arithmetic allows accessing elements by offset.
Step 2: Trace the loop output
Pointer ptr points to arr[0]. *(ptr + i) accesses arr[i]. Loop prints 5, 10, 15 separated by spaces.