What is the output of the following code snippet? #include <stdio.h> #define…
2018
What is the output of the following code snippet?
#include <stdio.h>
#define var 3
void main()
{
short num[3][2] = {3, 6, 9, 12, 15, 18};
printf("%d %d", *(num+1)[1], **(num+2));
}
- A.
12 15
- B.
15 12
- C.
15 15
- D.
12 12
Attempted by 263 students.
Show answer & explanation
Correct answer: A
The output of the following C code is: 12 15
Explanation:
short num[3][2] = {3, 6, 9, 12, 15, 18};
This creates a 2D array with 3 rows and 2 columns:
num[0][0] = 3 num[0][1] = 6
num[1][0] = 9 num[1][1] = 12
num[2][0] = 15 num[2][1] = 18
Now evaluate the printf statement:
printf("%d %d", (num+1)[1], *(num+2));
1. *(num+1)[1]
- num points to the first row of the array.
- num + 1 points to the second row → num[1]
- (num+1)[1] refers to num[1][1]
- num[1][1] = 12
- * dereferences the value, so the result is 12
2. **(num+2)
- num + 2 points to the third row → num[2]
- *(num+2) gives the base address of row num[2]
- **(num+2) accesses the first element of that row
- num[2][0] = 15
Hence, the final output is:
12 15