Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is the difference between #define and typedef in C?

Status
Not open for further replies.

sacrpio

Member level 3
Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
One question on c

hi,
what is difference between #define & typedef

Thanks in advance
 

Re: One question on c

Hi
#define is for a MACRO
for example you may want to have NAME to be always replaced by your name "XYZ"

typedef is for variables.

B R M
 

One question on c

Hey sacrpio, your questions all sound like homework problems.
Tell us what you think the answers are, and the nice folks here will give you better help.
 

Re: One question on c

#define is macro which is used to define any text u want to replace

in ur sorce code if u #define equal = than in ur source code where

equal apears than it is replaced by = sign . u can use numacrical

expression in macros where typedef is way to define the variables


to ur own choice


like
typedef real float

also complex data structures can e defined by typedef satement
 
Re: One question on c

typedef is not for defining variables!
typedef is for defining types!
and you can make variables of a certain type (but you can make constants of a certain type too)

#define is a preprocessor statement as said before. It just substitutes the defined stuff in your code. It doesn't show up in the debugger! Try to avoid it as much as possible!!! Writing code in C for microcontrollers is one of the few places where it's actually interesting to use (especially when your compiler doesn't support 'inline' and 'const' doesn't put stuff in flash/rom (or you're short on resources))
 

One question on c

hi,
1. #define is used for MACROS. Example: Suppose we have a program where we need to use the value 3.14 many a times and for clarity sake we go for MACROS as
#define PI 3.14
So everytime u can use PI instead of 3.14.You cannot modify the value of PI.U can take it as read-only.
2.Coming to typedef we use it when we define our own datatypes.Structures and enums are user defined data types.Again coming to a math example u want to define your own data type called ComplexNumber you simply say
Code:
typedef struct { 
                         int real;
                         int img;
                     }ComplexNumber;
later u can say:
ComplexNumber num1;//(like int i)
without typedef u ll have to say:
struct ComplexNumber num1;
Regards,
Kareja
 

Re: One question on c

This is a basic question on C. You can find and learn more by down some C programming books from here.

Best regards,

otomi
 

Re: One question on c

Hi
Both the typedef and #define are used for easy and powerful usage of C language.

Both differ in conceptwise and usage wise.

Have a look in to a nice C book.


bye
Gopi
 

Re: One question on c

hi,
what is difference between #define & typedef

Thanks in advance

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> #define should not be terminated with semicolon, but typedef should be terminated with semicolon.

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

Here i got the answer: #define Vs typedef
 

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 should not be terminated with semicolon, but typedef should be terminated with semicolon.

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

5.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.

I got exact answer... #define vs typedef in C
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top