What will be the output of the following C code? #include void main() { int a[2][3] = {1, 2, 3, 4, 5}; int i = 0, j = 0; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) printf("%d", a[i][j]); }a.1 2 3 4 5 0b.1 2 3 4 5 junkc.1 2 3 4 5 5d.Run time error
Question
What will be the output of the following C code? #include void main() { int a[2][3] = {1, 2, 3, 4, 5}; int i = 0, j = 0; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) printf("%d", a[i][j]); }a.1 2 3 4 5 0b.1 2 3 4 5 junkc.1 2 3 4 5 5d.Run time error
Solution
The output of the given C code will be "1 2 3 4 5 0".
Here's the step by step explanation:
-
The 2D array 'a' is initialized with 5 values {1, 2, 3, 4, 5}. Since the array 'a' is of size 2x3 (2 rows and 3 columns), it can hold 6 values. But only 5 values are provided, so the last value will be automatically initialized to 0 by the C compiler.
-
The nested for loop is used to traverse through the array 'a'. The outer loop 'i' runs for 2 iterations (i=0 and i=1) representing the rows of the array. The inner loop 'j' runs for 3 iterations (j=0, j=1 and j=2) representing the columns of the array.
-
The printf statement inside the inner loop prints the value of the array at position [i][j].
-
So, in the first iteration when i=0 and j=0, a[0][0] is 1. This continues for all values of i and j, printing all the values of the array 'a' which are 1, 2, 3, 4, 5 and the last value which is automatically initialized to 0.
So, the correct answer is a. 1 2 3 4 5 0.
Similar Questions
what will be the output of the c program?
What will be the output of the following code?
What will be the output of the following C++ code?
What will be the output of the following program?
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
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.