Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
551 views
in Information Technology by (85.4k points)
closed by

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) ;

                print ("%d\n", a);

}

The output of the program is __________. 

1 Answer

0 votes
by (88.5k points)
selected by
 
Best answer

In the given program, in mystery function only addresses are swapped not the values. Values remain same as in the main function. Pointer swapping is local to the mystery function here.

In C language, parameters are passed by value even if they are pointer. So, there will be no change in the value of a, b, c and d.

Code Explanation:

int main () {

                int a = 2016, b = 0, c = 4, d = 42;                      // a= 2016, b= 0

                mystery (&a, &b);                                              // call void mysery (int *ptra, int *ptrb)

                if (a < c)

                mystery (&c, &a);

                mystery (&a, &d);

                printf (‘’%d\n’’, a);

}

void mysery (int *ptra, int *ptrb)                               // ptra = 2016, ptrb =0 during first call

{

                int *temp;

                temp = ptrb;

                ptrb = ptra;

                ptra = temp;                                                   // ptra = 0, ptrb = 2016,

}

But after this mystery(&a, &b); popped out of stack and all the pointers lost and all values wil remain same as original.

printf (‘’%d\n’’, a);                 // it will print the value of a i.e. 2016.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...