We all have definitely used typedef
s and #define
s one time or the other. Today while working with them, I started pondering on a thing.
Consider the below 2 situations to use int
data type with another name:
typedef int MYINTEGER
and
#define MYINTEGER int
Like above situation, we can, in many situations, very well accomplish a thing using #define, and also do the same using typedef, although the ways in which we do the same may be quite different. #define can also perform MACRO actions which a typedef cannot.
Although the basic reason for using them is the different, how different is their working? When should one be preferred over the other when both can be used? Also, is one guaranteed to be faster than the other in which situations? (e.g. #define is preprocessor directive, so everything is done way earlier than at compiling or runtime).