Tuesday, 29 May 2012

Hi friends,
Here i am going to discuss about macro functions in c++.

Macro function can be defined using "#define" directive as we do for constants.

One  advantage of  macros is that during preprocessing macro expressions are expandedi nline in our program  except those lines that contain '#' character as starting non-whitespace characters.

one ristrinction with macros is that we should define macros for only those lines that require only  small number of executing statments in during processing  of  expression.

However  if you want run macro.cpp file on your pc download from from here:

macro.cpp


so here is code for macro.cpp file.


#include <iostream.h>
#include <conio.h>
#define swap(a,b) do{ a^=b; b^=a; a^=b; } while ( 0 )
#define getmax(a,b) ((a>b)?a:b)

void main()
{
clrscr();
int x = 0;
int y = 1;
int z = 4;


if(x < y)
{
swap(x,y);
cout<<x<<" "<<y<<endl;
cout<<"max of x and y ="<<getmax(x,y)<<endl;
}
else
{
swap(x,z);
cout<<x<<" "<<z<<endl;


}
cout<<"max of x and z ="<<getmax(x,z)<<endl;
getch();
}


and here is the output in below image:

And here is the download link for macro.cpp file  if you want download and run in your c++ builder;
macro.cpp

So that's it enjoy it:
For another such c++ tutorials follow this link: