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 ?

  1. A.

    Compilation fails.

  2. B.

    Execution results in a run-time error.

  3. C.

    On execution, the value printed is 5 more than the integer value entered.

  4. 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 variable pi is declared and initialized to point to the memory address of the integer variable i.

  • scanf("%d", pi); The scanf function expects a memory address where it can store the user's input. Normally, you see this written as scanf("%d", &i). Because pi already holds the address of i (pi == &i), passing pi without the & operator is completely correct and valid. It stores the input directly into i.

  • printf("%d \n", i+5); This takes the value now stored in i, adds 5 to it, and prints the result.

Explore the full course: Tpsc Assistant Technical Officer