Write a program in C for swapping of two values using functions
#include<stdio.h>
#include<conio.h>
int swapval(int,int);
int swapref(int*,int*);
int a,b;
void main()
{
clrscr();
printf(“enter the two values\n”);
scanf(“%d%d”,&a,&b);
printf(“pass by value\n”);
printf(“before function call a=%d b=%d “,a,b);
swapval(a,b);
printf(“after function swapval a=%d b=%d “,a,b);
printf(“pass by reference\n”);
printf(“before function call a=%d b=%d “,a,b);
swapref(&a,&b);
printf(“after function swapref a=%d b=%d “,a,b);
getch();
}
swapval(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf(“\nwith swap val x=%d y=%d”,x,y);
}
swapref(int*x,int*y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
printf(“\nwith swapref x=%d y=%d “,*x,*y);
}
Post a Comment