Monday, February 25, 2013

What is the difference between '#define' and 'typedef'?


(a) typedef keep the property of attributes whereas #define doesn’t. For example

typedef char *type_t;
#define char *type_d

type_t s1,s2;
type_d s3,s4;

In the above declarations, s1,s2 and s3 are all declared as char* but s4 is declared as a char, which is probably not the intention.

(b) #define keeps string attributes whereas typedef doesn’t.

#define int INT;
typedef int MYINT

unsigned MYINT a;
unsigned INT a; /* Illegal */

For the typedef scenario, it doesn't change to unsigned 'int a' but in #define case it works!!!.

No comments :