top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are the advantages of functions over macros; which are the cases when should we use Macros in C?

+1 vote
516 views
What are the advantages of functions over macros; which are the cases when should we use Macros in C?
posted Sep 2, 2015 by Ritika

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Primary drawback of macros is that it doesn't do type checking otherwise for all practical purpose I will prefer it over functions for the following reasons -

The function call requires a branch (jump to another address) + stack frame creation for a function. Both are costly operations.
Now a days, inline functions are preferred over macro in case the number of lines in the function are less than or equal to 20. The inline functions are inlined (no jump, no stack frame) + type checking (disadvantage of macro).

answer Sep 2, 2015 by Salil Agrawal
Similar Questions
0 votes

Used the sizeof of function, which gives 1; why?
I want to know the size of the entire function. How to achive it?

#include <stdio.h>
void (*p)(int); 
void test_func(int data)
{
  printf("%d\n",data);
}

main(void)
{
    p = test_func;
    (*p)(4);
    printf("%d",sizeof(test_func));
}
...