Write
a program in C to interchange the value of two variables by using the concept
of call by reference method.
In the call by reference method addresses of argument
are passed from main function to the function definition and these addresses
are collected or stored in pointer variables. If we update the value of
variables in function definition will be reflected throughout the program.
Program:
-
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void main()
{
int x,y;
clrscr();
printf("\n enter the first number=");
scanf("%d",&x);
printf("\n enter the second number=");
scanf("%d",&y);
swap(&x,&y);
printf("\n x= %d", x);
printf("\n y= %d", y);
getch();
}
Output:
-
Enter
the first number=
Enter
the second number=
X=
Y=
Comments
Post a Comment