tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 C Language > Basic > Swap using Macros

Swap using Macros 

This example shows swapping two numbers using macros.

File Name  :  
source/C/basic/swap.c 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
01#include<stdio.h>
02#include<string.h>
03 
04#define swap_macro(a,b) a=a+b;b=a-b;a=a-b;
05 
06int swap(int a, int b){   
07    a=a+b;b=a-b;a=a-b;
08    printf("inside swap x:%d  y:%d\n",a,b);
09}
10 
11int main()
12{
13    int x=2,y=8;
14    swap_macro(x,y);
15    printf("x:%d  y:%d\n",x,y);
16 
17    //changes to x,y wont effect them
18    //in main as these params are passed by value
19    swap(x,y);
20    printf("x:%d  y:%d\n",x,y);
21         
22    getch();
23    return 0;
24}

It gives the following output,
x:8  y:2
inside swap x:2  y:8
x:8  y:2



 
  


  
bl  br