473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Difference between Typedef and Define...in C ?

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 16497
RedSon
5,000 Expert 4TB
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 Expert 8TB
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
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 Expert 8TB
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
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
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 64KB
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,data2);

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
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...
10
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...
10
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...
12
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...
5
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
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...
1
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
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,...
2
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 -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.