Write a program to swap content of two variable using macro.
#include<stdio.h> #define SWAP(i,j,type) {type temp;\ temp = i,i = j,j = temp;} int main() { int i1 = 10,i2 = 20; char c1 = 'A',c2 = 'B'; float f1 = 5.5,f2 = 7.5; printf("Before swapping\n"); printf("i1 = %d i2 = %d\n",i1,i2); printf("c1 = %c c2 = %c\n",c1,c2); printf("f1 = %f f2 = %f\n",f1,f2); SWAP(i1,i2,int); SWAP(c1,c2,char); SWAP(f1,f2,float); printf("After swapping\n"); printf("i1 = %d i2 = %d\n",i1,i2); printf("c1 = %c c2 = %c\n",c1,c2); printf("f1 = %f f2 = %f\n",f1,f2); return 0; }
Output
Before swapping i1 = 10 i2 = 20 c1 = A c2 = B f1 = 5.500000 f2 = 7.500000 After swapping i1 = 20 i2 = 10 c1 = B c2 = A f1 = 7.500000 f2 = 5.500000