Monday, September 8, 2014

C++ #define Vs typedef

#define typedef
Is a preprocessor token: the compiler itself will never see it. Is a compiler token: the preprocessor does not care about it.
Obeys scoping rules just like variables. Stays valid until the end of the file (or until a matching undef).
Is a preprocessor directive used to define macros or general pattern substitutions. For eg. #define MAX 100, substitutes all occurrences of MAX with 100 Creates an "alias" to an existing data type. For e.g. typedef char chr;
            #define INTPTR int*
            ...
            INTPTR a, b;
            
After preprocessing, that line expands to
            int* a, b;
            
Only a will have the type int *; b will be declared a plain int (because the * is associated with the declarator, not the type specifier).
        typedef int *INTPTR;
        ...
        INTPTR a, b;
        
In this case, both a and b will have type int *.

No comments:

Post a Comment