Jump to content

Inttypes.h: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
(105 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{merge to|C data types|discuss=Talk:C_standard_library#Pages_for_each_function_and_WP:NOTMANUAL|date=October 2011}}
{{lowercase|title=inttypes.h}}
{{lowercase|title=inttypes.h}}
{{C Standard library}}
{{C Standard library}}


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. It defines a number of macros for use with the <code>printf</code> and <code>scanf</code> family of functions, as well as functions for working with the <code>intmax_t</code> type.
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 [[C preprocessor#Including files|includes]] the [[stdint.h]] header and defines a number of [[C_macro#Macro_definition_and_expansion|macros]] for using it with the [[printf]] and [[scanf]] family of [[function (computer science)|functions]], as well as functions for working with the <code>intmax_t</code> type.


==Naming Convention for Macros==
===Application Usage===
The [[C_macro#Macro_definition_and_expansion|macros]] defined in inttypes.h follow a regular pattern to simplify usage. The pattern followed is as follows <ref name="man">http://manpages.ubuntu.com/manpages/gutsy/man7/inttypes.h.7posix.html</ref> :


===== First three characters =====
The purpose of <inttypes.h> is to provide a set of integer types whose definitions are consistent across machines and independent of operating systems and other implementation idiosyncrasies. It defines, via typedef, integer types of various sizes. Implementations are free to typedef them as ISO C standard integer types or extensions that they support. Consistent use of this header will greatly increase the portability of applications across platforms.
* PRI for output format ([[printf]], [[fwprintf]], [[wprintf]], etc.)
* SCN for input format ([[scanf]], fwscanf, etc.)


===Rational===
===== Fourth character =====
* d for [[decimal]] formatting
* x for [[hexadecimal]] formatting
* o for [[octal]] formatting
* u for [[unsigned|unsigned int]] formatting
* i for [[int]]eger formatting

===== Remaining Characters =====
* N for N [[bit]] size assignment to the data type (Eg. 32 for 32-bit size for [[Int (computer science)|int)]], 16 for 16-bit size for [[Unsigned int|unsigned]] and so on)
* PTR for [[pointer (computing)|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.

== Dependency Graph==
The adjoining image shows the dependency graph for inttypes.h<ref>http://api.plexyplanet.org/inttypes_8h.html</ref>
[[File:Dependency graph for inttypes.png|300px|thumb|Dependency Graph]]
The following table gives syntax used for various data types listed in inttypes.h<ref>http://en.cppreference.com/w/cpp/types/integer</ref>:
{|class = "wikitable"
|-
!Fixed width integer || signed || unsigned
|-
|8 bit || int8_t || uint8_t
|-
|16 bit || int16_t || uint16_t
|-
|32 bit || int32_t || uint32_t
|-
|64 bit || int64_t || uint64_t
|}
{|class = "wikitable"
|-
!Small & fixed integer types || signed || unsigned
|-
|8 bit || int_least8_t || uint_least8_t
|-
|16 bit || int_least16_t || uint_least16_t
|-
|32 bit || int_least32_t || uint_least32_t
|-
|64 bit || int_least64_t || uint_least64_t
|}
{|class = "wikitable"
|-
!Fast & fixed integer types || signed ||unsigned
|-
|8 bit || int_fast8_t || uint_fast8_t
|-
|16 bit || int_fast16_t || uint_fast16_t
|-
|32 bit || int_fast32_t || uint_fast32_t
|-
|64 bit || int_fast64_t || uint_fast64_t
|}

==Rationale==
{{Merge to|stdint.h|discuss=Talk:Inttypes.h#stdint.h|date=October 2011}}
The difference in [[Processing speed|processing speeds]] in different [[Microprocessor|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 [[Programming language|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, expect 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, int, short and long data types, respectively. In 32-bit systems, it was 8, 32, 16 and 32 bits for char, int, short and long data types<ref name="man"/>. 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 [[Integer (computer science)|int]] data type to a particular value(may be 16 bits or 32 bits)<ref>{{cite web|last=The Open Group Specifications Issue 6|title=Application Usage and Rationale|url=http://pubs.opengroup.org/onlinepubs/009604599/basedefs/inttypes.h.html|publisher=The IEEE and The Open Group}}</ref>. It can also be used to limit the size of data type modifiers like [[unsigned int]] and [[int|signed]] to specific values by using the [[macros]] listed in the [[header file]].<ref>{{cite web|last=The Open Group Specifications Issue 6|title=Application Usage and Rationale|url=http://pubs.opengroup.org/onlinepubs/009604599/basedefs/inttypes.h.html|publisher=The IEEE and The Open Group Base|accessdate=14 September, 2011}}</ref>


The ISO/IEC 9899:1990 standard specified that the language should support four signed and unsigned integer data types- char, short, int, and long- but placed very little requirement on their size other than that int and short be at least 16 bits and long be at least as long as int and not smaller than 32 bits. For 16-bit systems, most implementations assigned 8, 16, 16, and 32 bits to char, short, int, and long, respectively. For 32-bit systems, the common practice has been to assign 8, 16, 32, and 32 bits to these types. This difference in int size can create some problems for users who migrate from one system to another which assigns different sizes to integer types, because the ISO C standard integer promotion rule can produce silent changes unexpectedly. The need for defining an extended integer type increased with the introduction of 64-bit systems.
==See also==
==See also==
* [[C syntax#Integral types|Integral types]] in [[C syntax]]
* [[C syntax#Integral types|Integral types]] in [[C syntax]]
* [[stdint.h]]
* [[stdint.h]]
* [[wchar.h]]


==References==
==References==

*{{man|bd|inttypes.h|SUS|fixed size integer types}}
<references/>


==External links==
==External links==
*{{man|bd|inttypes.h|SUS|fixed size integer types}}
* [http://msinttypes.googlecode.com/svn/trunk/inttypes.h inttypes.h] header file for [[Visual C++]] (requires a compatible [[stdint.h]]).
* [http://msinttypes.googlecode.com/svn/trunk/inttypes.h inttypes.h] header file for [[Visual C++]] (requires a compatible [[stdint.h]]).
* [http://techforb.blogspot.com/2007/10/c-interger-constant-out-of-range.html Defining literals and variables of explicit types]
* [http://en.cppreference.com/w/cpp/utility/types/integer C++ reference for fixed width integer types inherited from C]
* [http://en.cppreference.com/w/cpp/utility/types/integer C++ reference for fixed width integer types inherited from C]
* [http://www.qnx.com/developers/docs/6.4.1/dinkum_en/c99/inttypes.html Macro definitions and their interpretation in inttypes.h ]

* [http://h21007.www2.hp.com/portal/site/dspp/menuitem.863c3e4cbcdc3f3515b49c108973a801/?jumpid=reg_R1002_USEN&ciid=a868e67018695110VgnVCM100000275d6e10RCRD#AUTO3 Writing machine-independent code]
* [http://www.opensource.apple.com/source/gcc/gcc-934.3/inttypes.h Apple's open source code for inttypes.h]
[[Category:C standard library headers]]
[[Category:C standard library headers]]



Revision as of 05:42, 7 October 2011

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 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 int), 16 for 16-bit size for unsigned 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.

Dependency Graph

The adjoining image shows the dependency graph for inttypes.h[2]

File:Dependency graph for inttypes.png
Dependency Graph

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

Fixed width integer signed unsigned
8 bit int8_t uint8_t
16 bit int16_t uint16_t
32 bit int32_t uint32_t
64 bit int64_t uint64_t
Small & fixed integer types signed unsigned
8 bit int_least8_t uint_least8_t
16 bit int_least16_t uint_least16_t
32 bit int_least32_t uint_least32_t
64 bit int_least64_t uint_least64_t
Fast & fixed integer types signed unsigned
8 bit int_fast8_t uint_fast8_t
16 bit int_fast16_t uint_fast16_t
32 bit int_fast32_t uint_fast32_t
64 bit int_fast64_t uint_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, expect 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, int, short and long data types, respectively. In 32-bit systems, it was 8, 32, 16 and 32 bits for char, int, short 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 to specific values by using the macros listed in the header file.[5]

See also

References

  1. ^ a b http://manpages.ubuntu.com/manpages/gutsy/man7/inttypes.h.7posix.html
  2. ^ http://api.plexyplanet.org/inttypes_8h.html
  3. ^ http://en.cppreference.com/w/cpp/types/integer
  4. ^ The Open Group Specifications Issue 6. "Application Usage and Rationale". The IEEE and The Open Group.{{cite web}}: CS1 maint: numeric names: authors list (link)
  5. ^ The Open Group Specifications Issue 6. "Application Usage and Rationale". The IEEE and The Open Group Base. Retrieved 14 September, 2011. {{cite web}}: Check date values in: |accessdate= (help)CS1 maint: numeric names: authors list (link)