Showing posts with label Preprocessors. Show all posts
Showing posts with label Preprocessors. Show all posts

Macros with Arguments

Have a look at the following code:


  #include<iostream.h>

#define MAX 10

void main(void)
{
for(int i=1;i<=MAX;i++)
cout<<i<<endl;
}

Above we have used the type of macro expansion that we learnt in the article

Introduction to C++ Preprocessor Directives


Do you know that just like functions we can have arguments in the macros too!
The following code shows how:


  #include<iostream.h>

// this is how macros with
// arguments are defined
// NOTE: THERE SHOULD NOT BE
// ANY SPACE BETWEEN THE
// MACRO TEMPLATE (i.e. AREA)
// AND THE PARANTHESIS
// CONTAINING THE ARGUMENTS
// (i.e. (x))

#define AREA(x) (x*x)

void main(void)
{
// calling a macro
// is almost similar
// to calling a function
cout<<AREA(10);
}

It is that simple! However, keep one thing in mind as stated in the comments
not to put any space between the macro template and the braces containing the
argument.


   #define AREA(x) (x*x) //correct
#define AREA (x) (x*x) // incorrect

Why use Macros


While function can do the same thing, still there are times when macros preferred
over function.


As a general rule, using macros increases the execution speed of a program
but at the same time increase the file size of the program. On the other hand
using functions decreases the speed of execution of program but also decreases
the file size.


Therefore, macros are used in time critical programs where execution speed
must be as fast as possible no matter what the file size is, in all other cases
functions are generally used.


Good-Bye!


Related Articles:


Introduction
to C++ Preprocessor Directives

Introduction to C++ Preprocessor Directives

As the name suggest, C++ Preprocessor is an integrated program in C++ that
preprocesses every program we write before compilation.


Preprocessor commands or directives are special functions that are executed
or processed before the compilation of the program.


Although we can write program without the knowledge of preprocessor directives
but why underutilize features when we have them?


Every preprocessor directive starts with a # symbol. While
we can have them anywhere in the program but they are almost always used at
the starting of the program before any function is defined.


Knowingly or unknowingly, we have been using one preprocessor directive. It
is the #include directive, which is most commonly used to include
header files in the program.


While there are many types of preprocessor directives, here we will only be
discussing about Macro Expansion directives and that also in the simplest sense!


Macro Expansion Directives


Have a look at the code below:


  #define MAX 10
cout<<MAX;

Here, MAX is known as ‘Macro Template’ and 10,
Macro Expansion’.


Upon preprocessing every instance of the word MAX is replaced with 10 and only
then is the program executed.


It is just like search and replace, in which every word defined as macro template
(in this case MAX) is replaced by the corresponding macro expansion (in this
case 10).


It is the common way of defining constants that doesn’t needs to be change
throughout.


A few points to note:




  • Please note that it is necessary not to end directives with semicolon (;).




  • While it is not necessary to use ALL-CAPS for macro template but it has
    become the standard.




Below is a simple example program that illustrates macro expansion directives:


  // example program in c++ to show
// the use of macro directives
#include<iostream.h>
// below is the macro, which can be
// changed to change the behaviour
// of the whole program
// DON'T END WITH ';'
#define MAX 3

void main(void)
{
int arr[MAX];
int i;

cout<<"enter elements of the array:";
for(i=0;i<MAX;i++)
cin>>arr[i];

cout<<"elements are:";
for(i=0;i<MAX;i++)
cout<<"\n"<<i<<":"<<arr[i];
}

Good-Bye!


Related Articles:


Check out this stream