Macro and inline function are very similar. Compiler expands macro where it is called and inline functions are also copied to the place where it is called. They both increase code size as compiler copies object code to the location where these are getting called. However they are not same and differences between macro and inline functions are:

Typed arguments vs untyped arguments

Inline functions take defined typed arguments where as macros can take any type as argument.

Example syntax

inline int swap(int *a, int *b);
#define swap(a, b) 

Macros are expanded by the preprocessor before compilation takes place. Compiler will refer error messages in expanded macro to the line where the macro has been called.

Compilation error in macro

int current_user;
int next_user;
#define set_userid(x)\
current_user = x \
next_user = x + 1; 
int main (int argc, char *argv[])
{
10    int user = 10;
11    set_userid(user);
12  }
main.c(11):error C2146: syntax error : missing ';' 
          before identifier 'next_user'

Line 5 has the original compilation error

Compilation error in inline function

int current_user;
int next_user;
inline void set_userid(int x)
{
  current_user = x 
  next_user = x + 1; 
}
int main (int argc, char *argv[])
{
10    int user = 10;
11    set_userid(user);
12  }
main.c(6) : error C2146: syntax error : missing ';' before identifier 'next_user'

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

parameter passing call-by-value, parameter passing call-by-reference, structure as function argument, array as function argument, inline-functions, macro vs inline-function, function pointer, default C++ argument value, Recursion,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan