473,406 Members | 2,620 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,406 software developers and data experts.

why typedef ?

In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

can i write a portable code without using all these typedefs ?

thanx in advance for any help.....
Nov 14 '05 #1
8 5150

"junky_fellow" <ju**********@yahoo.co.in> wrote in message
news:8c*************************@posting.google.co m...
In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

Helps in porting of applications to different architecture set.
i.e for example - at some point of time if you come to know that 'unsigned
int' is now of 16 bits than 32 bits, and 'short' is 32 bit than 16bits !!!
then there will be less headache in your code (minimal code
rewrites/changes) all you have to do now is to redeclare the typedef
keeping t_uint32 as is.
typedef short t_uint32;

This was just one use i thought, others in this group might be more clearer
with other examples/usages.
In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

can i write a portable code without using all these typedefs ?
The whole purpose of this typedef is to make your life easier so use
them..
thanx in advance for any help.....

Nov 14 '05 #2
junky_fellow <ju**********@yahoo.co.in> scribbled the following:
In most of the codes that i have seen,
i observed the following typedefs. typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8 what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ? In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?
It makes the code more portable by abstracting away the underlying
types. You now have types that can suit a particular requirement, no
matter what concrete types satisfy that requirement. You can use the
types "t_uint32" etc. in thousands of places in your program, and if
you should ever encounter a platform where an unsigned 32-bit integer
is not unsigned int, all you have to change is one line.
can i write a portable code without using all these typedefs ?
Yes, but it will be much more tiresome.
thanx in advance for any help.....


You're welcome.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Insanity is to be shared."
- Tailgunner
Nov 14 '05 #3
ju**********@yahoo.co.in (junky_fellow) wrote:
In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?


Laziness and a misplaced idea of "being low-level", I guess. Uusually
Nothing rational.
Exception: if this code is used for communication between programs which
might be compiled with different integer sized. In that case, an #if'ed
sequence of #define <type_for_this_implementation> <required_size_type>
is useful. For single-program use, not.

Richard
Nov 14 '05 #4
junky_fellow wrote:

In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
Those typedefs are pretty braindead.
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?
It does become more portable if you use the 1999 ISO C standard
types which are defined in stdint.h, name int8_t, uint8_t etc.
can i write a portable code without using all these typedefs ?


As long as you don need portability across everything
from supercomputers to micro controllers then yes. If
you pick a subset like "All 32 bit OSes" then its
relatively easy.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+

GPLG
GPLGPLGP
GPLGPLGPLGP
GPLGP
GPL MICROSOFT
GPLGP
GPLGPLGPLGP
GPLGPLGPL
GPLGPL
Nov 14 '05 #5


junky_fellow wrote:

In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

can i write a portable code without using all these typedefs ?

thanx in advance for any help.....


Click this links:
http://groups.google.com/groups?hl=e....com&frame=off
http://groups.google.com/groups?hl=e...sdi.com&rnum=3

http://groups.google.com/groups?hl=e...sdi.com&rnum=9
http://groups.google.com/groups?hl=e...guy.com&rnum=2

http://groups.google.com/groups?hl=e...4ax.com#link10

[More if you type "typedef" in google's comp.lang.c search]
--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
Nov 14 '05 #6
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in message news:<cf**********@oravannahka.helsinki.fi>...
junky_fellow <ju**********@yahoo.co.in> scribbled the following:
In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?


It makes the code more portable by abstracting away the underlying
types. You now have types that can suit a particular requirement, no
matter what concrete types satisfy that requirement. You can use the
types "t_uint32" etc. in thousands of places in your program, and if
you should ever encounter a platform where an unsigned 32-bit integer
is not unsigned int, all you have to change is one line.
can i write a portable code without using all these typedefs ?


Yes, but it will be much more tiresome.
thanx in advance for any help.....


You're welcome.


thank you all for the useful tips and help...
Nov 14 '05 #7

"junky_fellow" <ju**********@yahoo.co.in> wrote

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
It says that this variable must be exactly 8 or 32 bits wide.
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?
Speed of typing is an extremely poor reason for doing this. The main reason
is if the size of underlying types changes when code is ported.
In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?
Let's say we're storing 32 bit RGBA images. If we move to a platform where
"long" is 64 bits, our code will work, but will waste half the memory. Since
images are often very large, this may not be acceptable. However if we use
t_unint32, and simply redefine it as an int (ints being 32 bits, lets
suppose), the code should be fine.
can i write a portable code without using all these typedefs ?

Usually the places you need a fixed size are few and far between. Usually
you only need a minimum size, and this is given by the C standard. So your
code shouldn't be littered with defined types. The exception is when a third
party has chosen to do just that, often to make it harder to port platforms
to other platforms, or because psychologically it says "my library is so
important it defines even the basic types you use", or occasionally because
the code isn't written in C and there is a genuine technical reason. If you
are using a library that makes heavy use of defined types it is reasonable
to do likewise.
Nov 14 '05 #8
boa
Malcolm wrote:
"junky_fellow" <ju**********@yahoo.co.in> wrote

[snip]
In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?


Let's say we're storing 32 bit RGBA images. If we move to a platform where
"long" is 64 bits, our code will work, but will waste half the memory. Since
images are often very large, this may not be acceptable. However if we use
t_unint32, and simply redefine it as an int (ints being 32 bits, lets
suppose), the code should be fine.


Another reason may be that you share data between platforms, then it may
be necessary to used fixed size datatypes.

boa@home
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
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...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
16
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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
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...

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.