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[])
{
  int user = 10;
  set_userid(user);
}
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[])
{
  int user = 10;
  set_userid(user);
}
main.c(6) : error C2146: syntax error : missing ';' before identifier 'next_user'

About our authors: Team EQA

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

#