To answer your question, apart from that, there is only one crucial difference. The function is stored in the binary and called, whereas the definition will be replaced at compile time. For things like this, where function < function overhead, it's better to go either the inline route, or use a #define like you did.
You cannot take the address of the macro, i.e. you cannot pass it's function pointer to some other function.
Inlining of the function by the compiler only works when the function is defined in the translation unit, i.e. if the compiler sees only the function declaration (e.g. in some "utils.h"), it cannot inline it.
The user can undefine the macro.
The macro "vanishes" in the binary, i.e. when you're debugging the code, you don't see AND anymore, but only the expanded code.
Every good solution is obvious once you've found it.
Playing with defines can be fun. You can set up your header files so each time they're included a different set on functions are included.
If you have one main header file which includes multiple header files, it may take a while to compile each of the source files which includes the main header. In this case you could set up a system where by you use "define" which parts you would not like included.
MessiahAndrw wrote:If you have one main header file which includes multiple header files, it may take a while to compile each of the source files which includes the main header. In this case you could set up a system where by you use "define" which parts you would not like included.
How about, not including a main header file to begin with?
int get_a(void)
{
printf("get_a()\n");
return 5;
}
int get_b(void)
{
printf("get_b()\n");
return 2;
}
int main(void)
{
printf("do_something() == %d\n", do_something(get_a(), get_b());
return 0;
}
If you run the code you're going to get different results. Use function-like macros carefully. Also, inlined function enable the compiler to perform type checks.
Last edited by TomTom on Thu Nov 08, 2007 10:10 pm, edited 1 time in total.