Consider the following code snippet. What is the output?#include <stdio.h>int main() { int arr[5] = {1, 2}; for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0;}
Question
Consider the following code snippet. What is the output?#include <stdio.h>int main() { int arr[5] = {1, 2}; for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0;}
Solution
The output of the code will be:
1 2 0 0 0
Here's the step by step explanation:
-
An integer array of size 5 is declared and initialized with the values 1 and 2 at the first two indices. The rest of the indices are not explicitly initialized.
-
In C, when an array is partially initialized, like in this case, the rest of the elements get automatically initialized to 0.
-
A for loop is used to iterate over the array from index 0 to 4.
-
Inside the loop, the value at each index of the array is printed.
-
Therefore, it prints 1 and 2 for the first two indices and 0 for the rest of the indices.
Similar Questions
What is the output for the following code?
What does the following code snippet do?
What is the output of the following code in Python?
What is the output for the following code?Note: This question helps in clearing conceptual technical interviews for service-based companies.
What will be the output of the following C++ code?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.