Alex Rivera | Logout

Why do C programmers use typedefs to rename basic types?

Asked 2010-07-27T04:57:04.887
56

So I'm far from an expert on C, but something's been bugging me about code I've been reading for a long time: can someone explain to me why C(++) programmers use typedefs to rename simple types? I understand why you would use them for structs, but what exactly is the reason for declarations I see like

typedef unsigned char uch;
typedef uch UBYTE;
typedef unsigned long ulg;
typedef unsigned int u32;
typedef signed short s16;

Is there some advantage to this that isn't clear to me (a programmer whose experience begins with Java and hasn't ventured far outside of strictly type-safe languages)? Because I can't think of any reason for it--it looks like it would just make the code less readable for people unfamiliar with the project.

Feel free to treat me like a C newbie, I honestly know very little about it and it's likely there are things I've misunderstood from the outset. ;)

c++ c
Edit
Report

4 Answers

14

This allows for portability. For example you need an unsigned 32-bit integer type. Which standard type is that? You don't know - it's implementation defined. That's why you typedef a separate type to be 32-bit unsigned integer and use the new type in your code. When you need to compile on another C implementation you just change the typedefs.

answered 2010-07-27T04:59:49.640
0

We use it to make it Project/platform specific, everything has a common naming convention

pname_int32, pname_uint32, pname_uint8 -- pname is project/platform/module name

And some #defines

pname_malloc, pname_strlen

It easier to read and shortens long datatypes like unsigned char to pname_uint8 also making it a convention across all modules.

When porting you need to just modify the single file , thus making porting easy.

answered 2010-07-27T05:05:19.643
0

To cut the long story short, you might want to do that to make your code portable (with less effort/editing). This way you don't depend to 'int', instead you are using INTEGER that can be anything you want.

answered 2010-07-27T05:25:43.657
0

All [|u]intN_t types, where N=8|16|32|64 and so forth, are defined per architecture in this exact manner. This is a direct consequence of the fact that the standard does not mandate that char,int,float, etc. have exactly N bits - that would be insane. Instead, the standard defines minimum and maximum values of each type as guarantees to the programmer, and in various architectures types may well exceed those boundaries. It is not an uncommon sight.

The typedefs in your post are used to defined types of a certain length, in a specific architecture. It's probably not the best choice of naming; u32 and s16 are a bit too short, in my opinion. Also, it's kind of a bad thing to expose the names ulg and uch, one could prefix them with an application specific string since they obviously will not be exposed.

Hope this helps.

answered 2010-07-27T05:57:50.833

Your Answer