typedef vs 23define

Solutions on MaxInterview for typedef vs 23define by the best coders in the world

showing results for - "typedef vs 23define"
Hamza
01 Nov 2020
1typedef char* ptr;
2ptr a, b, c;
3the statement effectively becomes
4
5char *a, *b, *c;
6This declares a, b, c as char*.
7
8In contrast, #define works like this:
9
10#define PTR char*
11PTR x, y, z;
12the statement effectively becomes
13
14char *x, y, z;
Sara
25 Oct 2017
1**Difference**
2
31) 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.
42) typedef interpretation is performed by the compiler where #define statements are performed by preprocessor.
53) #define should not be terminated with a semicolon, but typedef should be terminated with semicolon.
64) #define will just copy-paste the definition values at the point of use, while typedef is the actual definition of a new type.
75) 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).