difference between typedef and #define code example

Example 1: typedef vs #define

typedef char* ptr;
ptr a, b, c;
the statement effectively becomes

char *a, *b, *c;
This declares a, b, c as char*.

In contrast, #define works like this:

#define PTR char*
PTR x, y, z;
the statement effectively becomes

char *x, y, z;

Example 2: typedef vs #define

**Difference**

1) typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc.
2) typedef interpretation is performed by the compiler where #define statements are performed by preprocessor.
3) #define should not be terminated with a semicolon, but typedef should be terminated with semicolon.
4) #define 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).

Tags:

C Example