Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I encountered someones code which has following typedefs:
typedef unsigned short          u16_t;
typedef int                     s32_t;
typedef unsigned int            u32_t;

Why is it needed? For readability purposes?

1 solution

C and C++ original types do not guarantee a certain size. For example, there is no guarantee for how long an int is. That was a wise design decision of the fathers of the language, but has also its draw backs. For example if you want to describe a data structure in which certain items must have certain lengths. For that purpose, many developers have started to write typedefs like the ones you have shown. Some of them are useful (for size compliance or as simple short hand), others undermine the original language concept and try to enforce certain type sizes. In many cases that is counter-productive. For example forcing an int to 16-bits makes sense on a 16-bit CPU. But on 32-bit or 64-bit this will most likely produce slower code.
Share this answer
Dan page 19-Jun-13 4:56am    
I think the one I showed is just for "Short Hands" because I can't see how the code I showed, enforces int to be 32 bits for example?
nv3 19-Jun-13 5:32am    
Perhaps this code is in an include file which is supposed to be adapted for each platform. And for s32_t the adaption would choose between short int, int, long int etc. depending on the particular platform.

It could be both: A definition to make some data structures compatible between platforms (e.g. layout of structures in a binary file), or a somewhat clumsy attempt to make things "better" than what the C/C++ designers had in mind as basic types.

I would leave it in anyway, because otherwise you will probably have to edit hundreds or thousands of lines of code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900