Difference

R

raghu

can anyone please tell me difference between the two statements.

#define ME int
and

typedef int ME

Both statements do the same work.. right?

Thanks a lot in advance.

Regards,
Raghu
 
S

santosh

raghu said:
can anyone please tell me difference between the two statements.

#define ME int
and

typedef int ME

Both statements do the same work.. right?

Thanks a lot in advance.

In this instance yes, but in general no. #define is a preprocessor
directive and is simply a text substitution feature. Besides simple
substitutions like above, it can also be used for function like macros
etc. typedef is meant expressly for the purpose of defining a type.
Also typedef is a feature of the compiler proper and is thus better
suited for it's job than a simple #define, though in many instances,
the latter may be the only way. Also a #define can be later undefined
with #undef, not so a typedef. You can use typedef with complex types
like struct.

In general, if you're trying to define an alias for an existing type go
with typedef. Use #define only when typedef will not do the job.
 
A

Andrew Poelstra

can anyone please tell me difference between the two statements.

#define ME int
and

typedef int ME

Both statements do the same work.. right?

typedef defines "ME" as a type. #define does a dumb replacement.
There are examples of usage where the two will behave differently,
but I'm too tired to think of one.
 
R

Richard Heathfield

Andrew Poelstra said:
typedef defines "ME" as a type. #define does a dumb replacement.
There are examples of usage where the two will behave differently,
but I'm too tired to think of one.

Let me help you out. The first one will behave differently by passing
through a compiler unscathed!

Okay, here's a more sensible example:

#define STRING char * /* DANGER, WILL ROBINSON!!!! */

vs.

typedef char *STRING; /* NOT QUITE SO DANGEROUS BUT STILL SCARY */


These will have different effects when the compiler is considering code such
as:

STRING x, y, z;

With the #define you get:

char *x, y, z; /* one pointer, two single chars */

With the typedef you get three pointers, of course.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
M

Mark McIntyre

can anyone please tell me difference between the two statements.

#define ME int
and

typedef int ME

Both statements do the same work.. right?

No.
The first is substituted into your code before compilation.
The second creates an alias for "int".
With the above definitions there's little difference between the two ,
but with a pointer, the difference is significant.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,092
Latest member
vinaykumarnevatia1

Latest Threads

Top