Friday, November 14, 2008

#define Vs typedef


#define Vs typedef

1. A typedef declaration does not create a new type, it provides an alias to an existing type. of course, typedef is like #define, the difference is typedefs are interpreted by the compiler, and it can cope with textual substitutions that are beyond the capabilities of the preprocessor whereas #defines are handled by preprocessor.

2. typedef does typechecking.
In case of #define it will replace the macro with macro definition and then checks the type during the compilaton.

#define CHARP char*
typedef char* CHAR_P;
CHARP ch1,ch2;
CHAR_P ch3,ch4;
ch1 = "Character1"; //legal
ch2 = "Character2"; //illegal
ch2 = 's'; //legal
ch3 = "Character3"; //legal
ch4 = "Character4"; //legal

3. It is difficult to use #define to define a structure ,union or a class.










0 comments:

Hit Counter


Statistics

Followers

 

Programmer's Guide. Copyright 2008 All Rights Reserved