< C Programming

The inttypes.h file is a C header file that is part of the C standard library and API. It was added with the 1999 version of the ISO C standard (known as C99). It includes the stdint.h header and defines a number of macros for using it with the printf and scanf family of functions, as well as functions for working with the intmax_t type.

Naming Convention and format specifiers for Macros

The macros defined in inttypes.h follow a regular pattern to simplify usage. The pattern followed is as follows [1] :

First three characters
Fourth character
Remaining Characters
  • N for N bit size assignment to the data type (Eg. 32 for 32-bit size for integer, 16 for 16-bit size for unsigned int and so on)
  • PTR for pointer
  • MAX for maximum supported bit size
  • FAST, whose meaning is not clearly defined and is left to the implementation to decide what is meant by a "fast" integer data type.

Syntaxes

The following table gives syntax used for various data types listed in inttypes.h[2][3]:

Fixed width integersignedunsigned
8 bitint8_tuint8_t
16 bitint16_tuint16_t
32 bitint32_tuint32_t
64 bitint64_tuint64_t
Small & fixed integer typessignedunsigned
8 bitint_least8_tuint_least8_t
16 bitint_least16_tuint_least16_t
32 bitint_least32_tuint_least32_t
64 bitint_least64_tuint_least64_t
Fast & fixed integer typessignedunsigned
8 bitint_fast8_tuint_fast8_t
16 bitint_fast16_tuint_fast16_t
32 bitint_fast32_tuint_fast32_t
64 bitint_fast64_tuint_fast64_t

Rationale

The difference in processing speeds in different processors like 16-bit, 32-bit and 64-bit systems, called for a uniform size for various data types. ISO/IEC 9899:1990 specified that the language should support basic data types like char, int, short and long but did not restrict the minimum or maximum size for these data types, except that int be at least 16-bits long and long be 32-bits long.

In 16-bit systems, most implementations assigned 8, 16, 16 and 32 bits for char, short, int and long data types, respectively. In 32-bit systems, it was 8, 16, 32 and 32 bits for char, short, int and long data types[1]. The difference in size of int caused problems to users who migrated from one system to another.

The main purpose of including this header file is to restrict, or in other words, limit the exact size of int data type to a particular value(may be 16 bits or 32 bits)[4]. It can also be used to limit the size of data type modifiers like unsigned int and signed int to specific values by using the macros listed in the header file.[5]

See also

References

  1. 1 2 http://manpages.ubuntu.com/manpages/gutsy/man7/inttypes.h.7posix.html
  2. http://en.cppreference.com/w/cpp/types/integer
  3. http://linux.die.net/man/3/int64_t
  4. The Open Group Specifications Issue 6. "Application Usage and Rationale". The IEEE and The Open Group. http://pubs.opengroup.org/onlinepubs/009604599/basedefs/inttypes.h.html.
  5. The Open Group Specifications Issue 6. "Application Usage and Rationale". The IEEE and The Open Group Base. http://pubs.opengroup.org/onlinepubs/009604599/basedefs/inttypes.h.html. Retrieved 14 September, 2011.
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.