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.
      Code:
      
      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:5:{
      Line:6:  int user = 10;
      Line:7:  set_userid(user);
      Line:7:}
       
      main.c(7):error C2146: syntax error : missing ';' 
                before identifier 'next_user'
      
      Line 2 has the original compilation error
      

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

enum type, macro, macro vs constant, enum vs macro, macro vs typedef, debug builds, debug macros, compiler macro for C++, FILE, LINE, DATE, TIME, compiler marcos,

# 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