Differences are:

    • Typedef defines a new data type.
    • Macros can be of any type. Macros can even be any code block containing statements, loops, function calls etc.
    • Syntax/Example of typedef:
      typedef unsigned short WORD;
      #define WORD unsigned short
    • 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.
      Line:1:#define set_userid(x)\
      Line:2:current_user = x \
      Line:3:next_user = x + 1; 
      Line:4:int main (int argc, char *argv[])
      Line:6:  int user = 10;
      Line:7:  set_userid(user);
      main.c(7):error C2146: syntax error : missing ';' 
                before identifier 'next_user'
      Line 2 has the original compilation error

About our authors: Team EQA

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

Learn on Youtube