What is the difference between Macro and typedef?

typedef is different from Macro among the following aspects

  1. typedef is limited to giving symbolic names to types only, whereas Macro can be used to define an alias for values as well, e.g., you can define 3.14 as PI, etc.
  2. typedef interpretation is performed by the compiler where Macro statements are performed by a preprocessor.
  3. A macro should not be terminated with a semicolon, but typedef should be terminated with a semicolon.
  4. Macro will just copy-paste the definition values at the point of use, while typedef is the actual definition of a new type.
  5. typedef follows the scope rule which means if a new type is defined in a scope (inside a function), then the new type name will only be visible till the scope is there. In case of #define, when preprocessor encounters #define, it replaces all the occurrences, after that (No scope rule is followed).

Comments