Why you should use stdint.h


Assume you are working on a project for a device where integers by default are 16 bits long (i.e. 16-bit architecture). You use the “int” type to manipulate 16-bit integers in your code. One day, the target hardware is updated and the default integer type “int” is now 32 bits long (i.e. 32-bit architecture). Your code will not be usable anymore because of the assumption you made about the 8-bit size of the “int” type.

To avoid this kind of tricky situations, the stdint.h header file allows you to use exact-width integer types. with stdint.h you can use the “int16_t” type instead instead of “int”. Wherever your code will be deployed, you are sure that all “int16_t” variables will be 16-bit integers.

So if you move your code from a hardware that has default 16-bit integers to one that has default 32-bit integers, your code will not have to be modified.


Using stdint.h integers


Exact width integers

The most known types defined in stdint.h are the exact width integer types, they are as follows

int8_t
int16_t
int32_t
uint8_t
uint16_t
uint32_t

The typedef name intN _t designates a signed integer type with width N, no padding bits, and a two's-complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits.

The typedef name uint N _t designates an unsigned integer type with width N. Thus, uint24_t denotes an unsigned integer type with a width of exactly 24 bits.


Fastest minimumn-width integer types

In addition to these types, stdint.h defines other useful types for performance boosting purposes. The fastest minimumn-width integer types designates an integer types that are usually fastest to operate with among all integer types that have at least the specified width. The use of this category of types is meaningful only in architectures where there is a correlation between performance integer width.

These types are as follows :

int_fast8_t
int_fast16_t
int_fast32_t
int_fast64_t
uint_fast8_t
uint_fast16_t
uint_fast32_t
uint_fast64_t

Other categories of integer types

There are other useful int types defined by stdint.h ( “Greatest-width integer types”, “Integer types capable of holding object pointers” and “Minimum-width integer types”), see the man page for more info.



Labels: , Wireless Internet Security Coding Network Monitoring

Comment

Enter your comment (wiki syntax is allowed):
PMHWS

Wireless Internet Security Performance RADIUS server Wireless Internet Security Performance RADIUS server