473,587 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between Typedef and Define...in C ?

26 New Member
whats the Difference between Typedef and Define in C?
we can Use both for the case below...

#define INT32 int

typedef int INT32;

for both the case actually what will happen when compiling and running?

And is there any specific use for typedef .. If yes give me Examples ?

Thanks & Regards
-Balaji
Apr 19 '07 #1
7 16508
RedSon
5,000 Recognized Expert Expert
I don't think you can do

#typedef lengthof(exp) ((sizeof((exp)) )/sizeof((*(exp)) ))

typedef only allows you to redefine types where as #define will let you define anything.
Apr 19 '07 #2
JosAH
11,448 Recognized Expert MVP
whats the Difference between Typedef and Define in C?
we can Use both for the case below...

#define INT32 int

typedef int INT32;

for both the case actually what will happen when compiling and running?

And is there any specific use for typedef .. If yes give me Examples ?

Thanks & Regards
-Balaji
A #define is just a macro, i.e. it will be processed/expanded by the preprocessor.
When the actual C compiler starts doing its work there will be no sign of any
INT32 anymore. The typedef language facility is handled by the C compiler
itself: it introduces another name for a type. It's an awful hack actually: when
the C compiler parses that name in the right context the name is supposed
the alias for the type, otherwise it's supposed to be an identifier name:
Expand|Select|Wrap|Line Numbers
  1. typedef int t;
  2. t t(t x) { int t; ... }
kind regards,

Jos
Apr 19 '07 #3
ngpbalaji
26 New Member
I don't think you can do

#typedef lengthof(exp) ((sizeof((exp)) )/sizeof((*(exp)) ))

typedef only allows you to redefine types where as #define will let you define anything.

The define will replace all the Macros before compile..but typedef will redefine types after compilation ...
Is it correct?
can u explain me on this?
Apr 19 '07 #4
JosAH
11,448 Recognized Expert MVP
The define will replace all the Macros before compile..but typedef will redefine types after compilation ...
Is it correct?
can u explain me on this?
Correct about the #define part. A typedef is just a new name for an already
existing type. defines are handled by the preprocessor while typedefs are
handled by the C compiler itself.

kind regards,

Jos
Apr 19 '07 #5
ngpbalaji
26 New Member
Correct about the #define part. A typedef is just a new name for an already
existing type. defines are handled by the preprocessor while typedefs are
handled by the C compiler itself.

kind regards,

Jos

Thanks for the kind Reply...
Regards
Balaji
Apr 19 '07 #6
mkyadav206
1 New Member
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.he typedef interpretation is performed by the compiler where as #define statements are processed by the pre-processor.

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.

Here I got exact answer.. Typedef vs Define in c
Feb 14 '15 #7
Sherin
77 New Member
Typedef :

A typedef defines a new name for existing types and does not introduce a new type. It is the (partial storage-class specifier) compiler directive mainly use with user-defined data types (structure, union or enum) to reduce their complexity and increase code readability and portability.

Syntax:

typedef type NewTypeName;

example

int main()
{
UnsignedInt data1, data2;

data1 = 100;

data2 = 200;

printf("%d %d ",data1,dat a2);

return 0;
}

Output: 100 200



define

A #define is a preprocessor directive and it replaces the value before compiling the code. One of the major problems with the macro that there is no type checking. Generally, the macro is used to create the alias, in C language macro is also used as a file guard.

example

int main()
{
printf("%d ", Value);

return 0;
}

Output: 10
Jul 1 '20 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

15
5641
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing everything in a /*...*/ comment and appending the 'hail world' code. Is there an easier, and, let's say, more
10
12201
by: Kenneth Brody | last post by:
Is there any way to know if there is a typedef of a given name? Specifically, I need to know if the compiler has a 64-bit integer type, and need to know if "int64_t" exists. Something like this pseudo-code: #if typedef(int64_t) typedef int64_t MY_BIG_INT #elif typedef(long long) typedef long long MY_BIG_INT #else
10
7493
by: O Plameras | last post by:
Are there differences in terms of functionality of, #define and typedef ? By the above I mean any instance when the outcome obtained by running two versions (1) and (2) below of C codes are different ?
12
8217
by: Thomas Carter | last post by:
Imagine that there is some include file f.h that contains the following line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it possible for the preprocessor to detect that ui32 already exists, when preprocessing F.c? The idea is that F.c will typedef ui32 as above if and only if such...
5
327
by: raghu | last post by:
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.
6
2257
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first access label are implicitly private; ifwe usethe struct keyword, then those members are public. Whether we define a class using the class keyword or the...
1
1984
by: drrngrvy | last post by:
Hi all, Consider: <> namespace a { namespace nested { class a_class {}; } } namespace b { using ::a::nested::a_class; } namespace c { typedef ::a::nested::a_class a_class; }
6
2142
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand, which is
2
4820
by: emitrax | last post by:
Hi there, is there any difference between using bit fields structure and plain variable plus macros for dealing with bits? Let's say I have a two bytes divided as follow 0 - 2: level 3 - 6: type 7 - 10: whatever
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.