Consider the following program segment : ```c #include <stdio.h> main() { int…
2024
Consider the following program segment : ```c
#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 integer value entered.
- D.
On execution, the value printed is 5 more than the address of variable i.
Attempted by 242 students.
Show answer & explanation
Correct answer: C
int *pi = &i;A pointer variablepiis declared and initialized to point to the memory address of the integer variablei.scanf("%d", pi);Thescanffunction expects a memory address where it can store the user's input. Normally, you see this written asscanf("%d", &i). Becausepialready holds the address ofi(pi == &i), passingpiwithout the&operator is completely correct and valid. It stores the input directly intoi.printf("%d \n", i+5);This takes the value now stored ini, adds 5 to it, and prints the result.