GATE 2014 C Programming Question 10

📘 GATE 2014 — C Programming | Question 10
Q.10. 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.

Solution

🔍 Let’s Analyze the Program Step by Step
First, let us look at the complete program and understand what each important statement does.
#include <stdio.h>

main()
{
    int i;
    int *pi = &i;

    scanf("%d", pi);
    printf("%d\n", i + 5);
}
💡 Key statements to notice
int i;   — declares an integer variable i.
int *pi = &i;   — declares pi as a pointer and stores the address of i in pointer pi.
🔹 Step 1: Variable Declaration
int i;
An integer variable i is created.
int *pi = &i;
pi is a pointer variable. It stores the address of i.
💡 Remember: & is an “address of” operator. Therefore, &i means the address of i.
📋 What do the variables contain?
Variable What it contains
i No value assigned yet
pi Address of i
pi   →   address of i   →   i
🔹 Step 2: Understanding scanf()
scanf("%d", pi);
The format specifier %d tells scanf() to read an integer. For %d, scanf() needs the address of an integer variable.
💡 Recall Step 1:
pi = &i
So, pi contains the address of i. Therefore, passing pi to scanf() provides the address of i.
Therefore, in this particular program:
scanf("%d", pi);
↓ because pi = &i ↓
scanf("%d", &i);
⌨️ Suppose the user enters:
10
Then scanf() stores 10 in variable i:
i = 10;
✅ Important: There is no compilation error or runtime error for valid integer input.
The value 10 is stored in i, not in pi.
🔹 Step 3: Understanding printf()
printf("%d\n", i + 5);
The printf() statement evaluates the expression i + 5 and prints the resulting integer.
💡 From Step 2:
Suppose the user entered 10. Therefore:
i = 10
i + 5  =  10 + 5  =  15
🖨️ Output: The value 15 is printed.
🔎 Checking the Options
(A) Compilation fails.
❌ False
The program compiles successfully.
(B) Execution results in a run-time error.
❌ False
pi points to a valid integer variable i, so scanf() can correctly store the input.
(C) The value printed is 5 more than the address of variable i.
❌ False
The program prints i + 5, not &i + 5.
(D) The value printed is 5 more than the integer value entered.
✅ True
If the input is x, then i = x. Therefore, the output is:
x + 5
🎯 Correct Answer: (D)
🧠 Simple Way to Remember
int *pi = &i;
scanf("%d", pi);
is equivalent to
scanf("%d", &i);
Why? pi already contains the address of i. Therefore, when scanf() receives pi, it stores the entered integer in i.
➜ Input = x   →   i = x   →   Output = x + 5

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »