Sunday 22 July 2012

Difference between functions and macro

We should not leave a space between the macro template and its argument in the macro definition.
#define SQUARE (x) (x*x)
int main()
{
int n,m;
printf("Enter the number to find the square:");
scanf("%d",&n);

m=SQUARE(n);

printf("Square of %d is %d",n, m);
}

The above program won’t run. It wouldn’t find the template for above call.
So be cautious of such mistakes.

Now let’s see the differences between macros and functions

Difference between Macros and Functions


1) In a macro call the macro template is replaced by the preprocessor with its macro expansion.
Whereas when a function is called the control is passed to the function along with its arguments and after performing operations on it the result is returned back.

2) If we make use of macros the program runs faster but the size of program increases.
Functions make the program smaller and compact.

3) If we want to make use of less memory space then we should use functions.
If we want our program to run faster we should use macros.
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: