C Program To Swap Two Integers Using Pointer
C Program :
/* Aim: To swap two numbers #include<stdio.h> void main() { int a=10,b=20; void swap1(int x,int y); void swap2(int *ptr1,int *ptr2); printf("\n Before swapping : a=%d b=%d ",a,b); swap1(a,b); printf("\n After swapping by swap1 : a=%d b=%d ",a,b); swap2(&a,&b); printf("\n After swapping by swap2 : a=%d b=%d \n \n",a,b); } void swap1(int x,int y) { int temp; temp=x; x=y; y=temp; } void swap2(int *ptr1,int *ptr2) { int temp; temp=*ptr1; *ptr1=*ptr2; *ptr2=temp; }
/* Output of above code:-
[root@ugilinux ~]# cc e11Sample1.c
[root@ugilinux ~]# ./a.out
Before swapping : a=10 b=20
After swapping by swap1 : a=10 b=20
After swapping by swap2 : a=20 b=10
*/