#include<stdio.h>
void f(int a)
{
printf("%d", a);
}
#define f(a) {}
int main()
{
/* call f : function */
}
How to call f
(the function)? Writing f(3)
doesn't work because it is replaced by {}
A macro is defined with the pre-processor directive. Macros are pre-processed which means that all the macros would be processed before your program compiles. However, functions are not preprocessed but compiled.
Macros are generally used to define constant values that are being used repeatedly in program. Macros can even accept arguments and such macros are known as function-like macros. It can be useful if tokens are concatenated into code to simplify some complex declarations.
Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called.
Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function.
Does (f)(3);
work?
The C preprocessor doesn't expand the macro f
inside ( )
.
int main()
{
#undef f // clear f!
f(3);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With