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.
#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.
0 comments: