All Exams Test series for 1 year @ ₹349 only
Question

Consider the following ANSI-C program.

#include <stdio.h>
int main(){
 int *ptr, a, b, c;
 a=5; b=11; c=20;
 ptr=&a; *ptr=c; ptr=&c;
 a=*(&b); c=*ptr-a;
 printf("%d",c);
 return(0);
}

The output of this program is ____________. (answer in integer)

Note: Assume that the program compiles and runs successfully.

C Program Output: Step-by-Step Execution Analysis

We trace the values of variables $a$, $b$, $c$, and the pointer $ptr$ throughout the program execution.

Initial Variable Values

  • $a = 5$
  • $b = 11$
  • $c = 20$
  • $ptr$ is uninitialized.

Pointer Assignments and Dereferencing

  1. $ptr = &a;$: The pointer $ptr$ now holds the memory address of variable $a$.
  2. $*ptr = c;$: The value of $c$ (which is 20) is assigned to the memory location pointed to by $ptr$. Since $ptr$ points to $a$, variable $a$ is updated.
    • $a$ becomes 20.
  3. $ptr = &c;$: The pointer $ptr$ now holds the memory address of variable $c$.
  4. $a = *(&b);$: This expression retrieves the value stored at the memory address of $b$ (which is 11) and assigns it to $a$.
    • $a$ becomes 11.
  5. $c = *ptr - a;$:
    • $*ptr$ dereferences $ptr$, which points to $c$. So, $*ptr$ evaluates to the current value of $c$ (20).
    • The expression becomes $c = 20 - a$.
    • Substituting the current value of $a$ (11), we get $c = 20 - 11$.
    • $c$ becomes 9.

Final Output

The $printf("%d", c);$ statement prints the final value of $c$.

  • The final value of $c$ is $9$.

Therefore, the program outputs $9$.

Was this answer helpful?

Important Questions from Pointer

  1. What would be the equivalent pointer expression for referring the array element ar[m][n][o]
  2. Only legal pointer operations:
    A. pointer + number $\rightarrow$ pointer
    B. pointer - number $\rightarrow$ number
    C. pointer + pointer $\rightarrow$ pointer
    D. pointer - pointer $\rightarrow$ pointer
    E. pointer - pointer $\rightarrow$ number
    Choose the most appropriate answer from the options given below:
  3. Consider the following C program:
    #include <stdio.h>
    void stringcopy(char *, char *);
    int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
    }
    void stringcopy(char *s, char *t) {
    while(*t)
    *s++ = *t++;
    }
    Which ONE of the following will be the output of the program?

  4. Consider the following C program:
    #include <stdio.h>
    int main(){
    int a;
    int arr[5] = {30,50,10};
    int *ptr;
    ptr = & arr[0] + 1;
    a = *ptr;
    (*ptr)++;
    ptr++;
    printf("%d", a + (*ptr) + arr[1]);
    return 0;
    }
    The output of the above program is ____________ (Answer in integer)

  5. The following statement in ‘C’
    int (*f())[ ];
    declares
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App