Consider the following program in C language: #include <stdio.h> main() { int…
2014
Consider the following program in C language:
#include <stdio.h>
main()
{
int i;
int*pi = &i;
scanf("%d",pi);
printf("%d\n", i+5);
}
Which one of the following statements is TRUE?
- A.
Compilation fails.
- B.
Execution results in a run-time error.
- C.
On execution, the value printed is 5 more than the address of variable i.
- D.
On execution, the value printed is 5 more than the integer value entered.
Attempted by 358 students.
Show answer & explanation
Correct answer: D
Answer: On execution, the value printed is 5 more than the integer value entered.
Explanation:
Variable i is declared, and the pointer pi is initialized to the address of i (pi = &i).
scanf("%d", pi) passes the address of i to scanf, so scanf writes the entered integer into the storage for i.
printf("%d\n", i+5) prints the current integer value stored in i plus 5. This is arithmetic on the integer value, not on the address.
Concrete example: if the user types 10 at the prompt, i becomes 10 and the program prints 15.
Notes:
If non-integer input is provided, scanf may fail to convert and i may remain uninitialized; that is input-handling behavior and can lead to unpredictable output.
For standards-compliant code, prefer declaring main as int main(void) and returning a value, but this does not change the described behavior of the program regarding input and output.