Computer Sciences > Gate 2014 Set-1 > C Code
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.

Correct : C Code

The program stores the integer entered by the user in variable i using the pointer pi. The program then calculates i + 5 and prints the result.
Solution:
pi = &i means that pi stores the address of i.
When scanf("%d", pi) is executed, the integer entered by the user is stored in i.
The statement printf("%d\n", i + 5) then prints the value of i + 5.
Example: If the user enters 10, then i = 10 and the output is 15.
Analyze the Options:
A) Compilation fails: Incorrect. The program is valid C code.
B) Execution results in a run-time error: Incorrect. The pointer correctly points to variable i.
C) The value printed is 5 more than the address of variable i: Incorrect. The program prints i + 5, not the address of i.
D) The value printed is 5 more than the integer value entered: Correct. The entered value is stored in i, and then 5 is added to it.
Correct Answer: D) On execution, the value printed is 5 more than the integer value entered.

Similar Questions

Consider the following two C code segments. Y and X are one and two dimensional arrays of size n and n × n respectively, where 2 ≤ n ≤ 10. Assume that in both c...
#25 MCQ
Consider the following C program segment. #include&lt;stdio.h&gt; int main( ) { &nbsp; &nbsp; char s1[7] = "1234", *p; &nbsp; &nbsp; p = s1 + 2; &nbsp; &nbsp;...
#37 MCQ
Consider the following function written in the C programming language. void foo (char *a) { &nbsp; &nbsp; if (*a && *a != ` `) &nbsp; &nbsp; { &nbsp;...
#49 MCQ

Related Topics

C language Pointers in C scanf printf C program output GATE Computer Sciences 2014 GATE CS 2014 Set-1 Q4 C compilation runtime error C pointer input

Unique Visitor Count

Total Unique Visitors

Loading......