Learn about Swap Two Number using a macro in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Swap Two Number using a macro in C
Initially, a function called SWAP(a,b,Type) is defined as a macro. Then the macro is called using the variables as the arguments in the function. Finally the final result is displayed.
Source code:
#include<stdio.h>
#define SWAP(a,b,Type) Type temp=a; a=b; b=temp;
int main()
{
int x=1, y=5;
SWAP(x,y,int);
printf("After Swapping \n x=%d \t y=%d\n",x,y);
return 0;
}
Output:
After Swapping
x=5 y=1
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Swapping program in C without using any temporary variable
C program to Swap using a temporary variable