What is the difference between typedef & Macros?

Showing Answers 1 - 9 of 9 Answers

Typedef  is used to create a new name to an already existing data type. Redefine the name creates conflict with the previous declaration.

eg:
typedef unsigned int UINT32

Macros [#define] is a direct substitution of the text before compling the whole code. In the given example, its just a textual substitution. where there is a posibility of redifining the macro

eg:
#define chPointer  char *
#undef chPointer
#define chPointer int *

  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

typedef defines a new type, in terms of existing types.  Macros are simply text replacements.

As an example of the difference, try the following:

#include <stdio.h>

typedef char * ptr;

#define PTR char *

int main(void)
{
   ptr a, b, c;
   PTR x, y, z;

   printf( "sizeof a: %un", (unsigned) sizeof(a) );
   printf( "sizeof b: %un", (unsigned) sizeof(b) );
   printf( "sizeof c: %un", (unsigned) sizeof(c) );
   printf( "sizeof x: %un", (unsigned) sizeof(x) );
   printf( "sizeof y: %un", (unsigned) sizeof(y) );
   printf( "sizeof z: %un", (unsigned) sizeof(z) );

   return 0;
}

Note that we used typedef to define a new type, ptr, which we then used to define variables a, b and c.  Also note we created a macro, PTR, which we then used to define variables x, y and z.  On my system, the results of the code are:

  sizeof a: 4
  sizeof b: 4
  sizeof c: 4
  sizeof x: 4
  sizeof y: 1
  sizeof z: 1

Note that y and z do _not_ have the same size as the x, whereas a, b and c are consistent.  Why is this?  Simple: we used a macro for x, y and z.

Look at what the macro actually does:

#define PTR char *

PTR x, y, z;

This is, in turn, converted (by simple text substitution) to:

char *x, y, z;

That line does _not_ define three pointer variables.  It defines three variables with a base type of "char", one of which - x - is a pointer.  It is the same as writing:

  char *x;
  char y;
  char z;

The typedef did not fail this way, because when we defined the new type, we defined it specifically as a _pointer_ _type_ - meaning that any variable defined with the new type will, in fact, be a pointer variable.

Other differences are more obvious, such as the macro's ability to do certain limited forms of evaluation, "stringizing" and so forth, none of which can be done by typedef... but I don't think those are relevant to the question you're actually asking here.

  Was this answer useful?  Yes

manoj yadav

  • Feb 13th, 2015
 

1>The typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values as well, like you can define 1 as ONE etc.

2>The typedef interpretation is performed by the compiler where as #define statements are processed by the pre-processor.

3>#define will just copy-paste the definition values at the point of use, while typedef is actual definition of a new type.

4>typedef follows the scope rule which mean 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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions