C Programming :: Basic Concepts - Discussion
30 / 90
#include<stdio.h>
int swap(int *a,int *b)
{
*a=*a+*b;*b=*a-*b;*a=*a-*b;
}
void main()
{
int x=10,y=20;
swap(&x,&y);
printf("x= %d y = %d\n",x,y);
}
#include<stdio.h>
int swap(int *a,int *b)
{
*a=*a+*b;*b=*a-*b;*a=*a-*b;
}
void main()
{
int x=10,y=20;
swap(&x,&y);
printf("x= %d y = %d\n",x,y);
}
Ax= 20 y = 10
Bx= 10 y = 20
CCompilation Error
DNone of these
Show Explanation
Asked In ::
Initially, x=10 and y=20.
Let us assume the memory address of x=10 is 1010 and that of y=20 is 2020.
When the function swap(&x,&y)...is called, the address of x and y is copied to the formal parameter thorough call by address.
Note: *a and *b means the values stored at the addresses(say, at 1010 value is 10 and at 2020 value is 20).
Inside the function definition-
*a=*a+*b
or, *a=10+20
or, *a=30
Next, *b=*a-*b
or, *b=30-20
or, *b=10
Finally, *a=*a-*b
or, *a=30-10
or, *a=20
Here, the changes made to the parameters will affect the passed arguments, due to which the changes will get reflected in the output.
Hence, the output is x= 20 y= 10.
Read Full Answer
Report Error
Please Login First Click Here