Jun 112012

What are the differences between typedef and #define in C or C++ languages ?


There are many differences between typedef & #define. The major differences are as follows:

1. Preprocessor v/s Compiler

#define is handled by the preprocessor, which will just copy-paste the #define values from the point of definition to the point of use. Where as typedef is handled by the compiler and is the actual definition of a new type. By the time control will reach the compiler, all the #define will be replaced.

Because of this difference, following can be the implications:

A) typedef should be terminated with semicolon, #define should not be terminated with semicolon.

B) There can be side effects of replacement in #define, For example:

        typedef char* string_t;
        #define string_d char*

        string_t s1, s2; // Both s1 and s2 are of type char*
        string_d s3, s4; // s3 is char* but s4 is of type char (and not char*)

The problem in second declaration is because preprocessor will replace it as

        char* s3, s4;

Which means that s3 will be of type char* but s4 will be of type char (* has to be specified with all the variables if we want all of them to be pointers like char *s3, *s4; ).

C)  typedef follows the scope rule. i.e 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. But when preprocessor encounters a #define, then it will replace all the occurences, after that (No scope rule is followed). For example:

        int main(){

            { // New scope STARTS
                typedef int myInt_t;
                #define myInt_d int

                myInt_t a;   // OK. a is of type int
                myInt_d b;   // OK. b is of type int
            }// New scope ENDS

            myInt_t  c;  // ERROR.. type myInt_t not found
            myInt_d  d;  // OK. d is of type int

2. Macro v/s type alias

#define can be used to define macros also, but typedef can only be used to provide a new name for already existing type (it cannot create a new type). similarly #define can used to define variables like

    #define N 10

This will not actually define N but will replace N with 10 in the entire file. so can be used for named-constants.

typedef can not be used for any other purpose but giving new name for already defined types.

3. Typedef as type alias

There are certain type definitions which you can only define using typedef and not #define. Consider the below cases:

A) If I want to give a new name for integer array of size 10, I can do so like:

        typedef int arr[10];

B) If you have more that one word in the type (like unsigned int).

        typedef unsigned int UINT;

We cannot define this type using #define

C) For giving new name to struct types, like

        typedef struct{
            int a;
            char b;
        } myType;

If you know about more differences feel free to comments 🙂


  5 Responses to “Difference between typpedef and #define”

Comments (5)
  1. These are very helpful…thank you:)

  2. Excellent post….

  3. nice post…………….

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>