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?
View worked solution
Answer: D
Let us analyze the execution of the given C program step-by-step:
- Variable Declaration:
This declares an integer variableint i;i. - Pointer Initialization:
This declares an integer pointerint* pi = &i;piand initializes it with the address ofi(i.e.,&i). Thus,pipoints to the memory location ofi. - Reading Input:
Thescanf("%d", pi);scanffunction expects the address of the variable where the input integer should be stored. Sincepiholds the address ofi(equivalent to&i), the integer entered by the user is successfully stored in the variablei. - Printing Output:
This prints the value stored inprintf("%d\n", i + 5);iplus .
Thus, the program compiles and executes successfully, printing a value that is more than the integer value entered by the user.
Therefore, the correct option is D.
Free practice · Your answers on this page are not saved.
