Skip to main content

GATE CSE 2016Programming in C › Functions & Parameter PassingVerified

GATE CSE 2016 PYQ: Consider the following C program. # include <stdio.h> void mystery ( int * ptra , int * ptrb ) { int * temp…

Consider the following C program.

# include <stdio.h>
void mystery (int *ptra, int *ptrb) {  
     int *temp; 
     temp = ptrb;  
     ptrb =ptra; 
     ptra = temp; 
} 
int main () { 
    int a = 2016, b=0, c= 4, d = 42; 
    mystery (&a, &b);
    if (a < c) 
          mystery (&c, &a); 
    mystery (&a, &d); 
    printf("%d\n", a); 
}

The output of the program is _________.

    Answer

    2016

    Approach

    The function mystery receives pointers by value. Inside it, only the local copies of the pointer variables are swapped; the integers in main are never modified. Therefore, the final value printed for a remains its initial value.

    Because mystery never dereferences its parameters to modify the pointed-to integers, and because the if condition is false, no value of a changes from its initial value 2016.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.