Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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 ?
    1. Compilation fails
    2. Execution results in a run-time error.
    3. On execution, the value printed is 5 more than the address of variable i.
    4. On execution, the value printed is 5 more than the integer value entered.
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.



Your comments will be displayed only after manual approval.