What is the output of the following C program? #include <stdio.h> int…
2023
What is the output of the following C program?
#include <stdio.h>
int main(void)
{
static float a[] = {13.24, 1.5, 4.5, 5.4, 3.5};
float *j;
float *k;
j = a;
k = a + 4;
j = j * 2;
k = k / 2;
printf("%f %f", *j, *k);
return 0;
}
- A.
13.25, 4.5
- B.
1.5, 3.5
- C.
13.24, 1.5, 4.5, 5.4, 3.5
- D.
Illegal use of pointer in main function
Attempted by 393 students.
Show answer & explanation
Correct answer: D
The declarations are:
float *j;
float *k;
So j and k are pointers to float, not float variables. Therefore:
j = a;
k = a + 4;
are valid pointer assignments.
The error occurs in these statements:
j = j * 2;
k = k / 2;
In C, pointer arithmetic allows adding or subtracting an integer offset from a pointer. Multiplication and division of pointers are not allowed. Therefore, both j * 2 and k / 2 are invalid pointer operations and the program gives a compile-time error.
If the intention were to modify the pointed values, the correct form would be:
*j = *j * 2;
*k = *k / 2;
Hence, the original code contains illegal pointer usage, so Option D is correct.
A video solution is available for this question — log in and enroll to watch it.