-
Consider the following program in C language :
#include
main ()
{
int i;
int *pi = &i;
scanf ("%d", pi);
printf ("%d\n", i + 5);
}
Which one of the following statements is TRUE ?
-
- Compilation fails
- Execution results in a run-time error.
- On execution, the value printed is 5 more than the address of variable i.
- On execution, the value printed is 5 more than the integer value entered.
- Compilation fails
Correct Option: D
We concentrate on following code segment :
→ int *Pi = & i;
Nothing wrong as ‘Pi’ is declared as integer pointer and is assigned the address of ‘i’ which is an “int”.
→ scanf (“%d”, Pi);
We know that scanf () has two arguments :
First is control string (“%d” in this case), telling it what to read from the keyboard.
Second is the address where to store the read item. So, ‘Pi’ refers to the address of “i”, so the value scanned by scanf () will be stored at the address of ‘i’.
Above statement is equivalent to :
scanf (“%d”, & i);
Finally the best statement :
printf (“%d\n”, i + 5); It prints the value 5 more than the value stored in i. So, the program executes successfully and prints the value 5 more than the integer value entered by the user.