A simple difference is the macros don't reach the compiler they are expanded in the pre processor itself. While the typedef is done during compile time. But there is more to it, this is just basic difference.
I am just adding one example of what anush said in the above comment... Suppose this is the code.
#include #define FUN int typedef int MOON;
int main(void) { unsigned MOON a = 20; /* will get error in this line. */ unsigned FUN b = 10; printf("The value of a %d\n",a); printf("The value of b %d\n",b); return 0; }
In this above code we can use MOON a, but we can,t use unsigned MOON a or short a.
You can't use any modifier like long, short etc.. with the typedef where as we can use modifiers with #deifne because they are getting processed before compilation.
A simple difference is the macros don't reach the compiler they are expanded in the pre processor itself. While the typedef is done during compile time. But there is more to it, this is just basic difference.
ReplyDeleteI am just adding one example of what anush said in the above comment...
ReplyDeleteSuppose this is the code.
#include
#define FUN int
typedef int MOON;
int main(void)
{
unsigned MOON a = 20; /* will get error in this line. */
unsigned FUN b = 10;
printf("The value of a %d\n",a);
printf("The value of b %d\n",b);
return 0;
}
In this above code we can use MOON a, but we can,t use unsigned MOON a or short a.
You can't use any modifier like long, short etc.. with the typedef where as we can use modifiers with #deifne because they are getting processed before compilation.