{{Short description|Data types supported by the C programming language}} {{Use dmy dates|date=July 2021}} {{More citations needed|date=July 2025}} {{C Standard Library}} In the C programming language, '''data types''' constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.
The C language provides basic arithmetic types, such as integer and real number types, and syntax to build array and compound types. The C standard library contains additional definitions of support types, that have additional properties, such as providing storage with an exact size, independent of the language implementation on specific hardware platforms.<ref>{{cite web|url=https://barrgroup.com/Embedded-Systems/How-To/C-Fixed-Width-Integers-C99|title=Portable Fixed-Width Integers in C|last=Barr|first=Michael|date=2 December 2007|access-date=18 January 2016}}</ref><ref name=c99>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 255, § 7.18 ''Integer types <stdint.h>''}}</ref>
==Primary types== ===Main types=== The C language provides the four basic arithmetic type specifiers {{code|char}}, {{code|int}}, {{code|float}} and {{code|double}} (as well as the Boolean type {{code|bool}}), and the modifiers {{code|signed}}, {{code|unsigned}}, {{code|short}}, and {{code|long}}. The following table lists the permissible combinations in specifying a large set of storage size-specific declarations. {| class="wikitable" |- ! Type ! Explanation ! Size (bits){{efn|The standard term for the size of an integer type in bits is ''width''. The width does not include padding bits.}}<!-- Shouldn't the article be updated to only use the term "width" with this meaning? --> ! Format specifier ! Range ! Suffix for decimal constants |- | {{code|bool}} || Boolean type, added in C23. (Previously {{code|_Bool}} added in C99,<ref>{{Cite web|title=Arithmetic types|url=https://www.cppreference.com/w/c/language/arithmetic_types.html|website=cppreference.com|access-date=15 September 2025}}</ref> but its size and its range were not specified.) || 1 (exact) || <code>%d</code> || {{bracket|{{mono|false}}, {{mono|true}}}} || {{n/a}} |- | {{code|char}} || Smallest addressable unit of the machine that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned. It contains {{code|CHAR_BIT}} bits.<ref name=c99sizes/> || ≥8 || <code>%c</code> || {{bracket|{{mono|CHAR_MIN}}, {{mono|CHAR_MAX}}}} || {{n/a}} |- | {{code|signed char}} || Of the same size as {{code|char}}, but guaranteed to be signed. Capable of containing at least the {{closed-closed|size=100%|−127, +127}} range.<ref name=c99sizes/>{{efn|name=restr|1=The minimal ranges {{closed-closed|size=100%|−(2<sup>''n''−1</sup>−1), 2<sup>''n''−1</sup>−1}} (e.g. [−127,127]) come from the various integer representations allowed by the standard (ones' complement, sign-magnitude, two's complement).<ref>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf | title=Rationale for International Standard—Programming Languages—C Revision 5.10 | at=p. 25, § 5.2.4.2.1 ''Sizes of integer types <limits.h>''}}</ref> However, most platforms use two's complement, implying a range of the form {{closed-closed|size=100%|−2<sup>''m''−1</sup>, 2<sup>''m''−1</sup>−1}} with {{math|''m'' ≥ ''n''}} for these implementations, e.g. [−128,127] (<code>SCHAR_MIN == −128</code> and <code>SCHAR_MAX == 127</code>) for an 8-bit {{mono|signed char}}. Since C23, the only representation allowed is two's complement, therefore the values range from at least {{closed-closed|size=100%|−2<sup>''n''−1</sup>, 2<sup>''n''−1</sup>−1}}.<ref name=c232compl>{{cite book | url=https://open-std.org/JTC1/SC22/WG14/www/docs/n3096.pdf| title=ISO/IEC 9899:2023 specification draft| at=p. 41, § 6.2.6 ''Representations of types''}}</ref>}} || ≥8 || <code>%c</code>{{efn|or <code>%hhi</code> for numerical output}} || {{bracket|{{mono|SCHAR_MIN}}, {{mono|SCHAR_MAX}}}}<ref>{{cite web |url=https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits?view=msvc-170| title=C and C++ Integer Limits | date=21 July 2023 }}</ref> || {{n/a}} |- | {{code|unsigned char}} || Of the same size as {{code|char}}, but guaranteed to be unsigned. Contains at least the {{closed-closed|size=100%|0, 255}} range.<ref name=c99generalrepr>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 37, § 6.2.6.1 ''Representations of types – General''}}</ref> || ≥8 || <code>%c</code>{{efn|or <code>%hhu</code> for numerical output}} || {{bracket|0, {{mono|UCHAR_MAX}}}} || {{n/a}} |- | {{Plainlist| * {{code|short}} * {{code|short int}} * {{code|signed short}} * {{code|signed short int}} }} | ''Short'' signed integer type. Capable of containing at least the {{closed-closed|size=100%|{{val|−32767}}, {{val|+32767}}}} range.<ref name=c99sizes>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 22, § 5.2.4.2.1 ''Sizes of integer types <limits.h>''}}</ref>{{efn|name=restr}} || ≥16 || <code>%hi</code> or <code>%hd</code> || {{bracket|{{mono|SHRT_MIN}}, {{mono|SHRT_MAX}}}} || {{n/a}} |- | {{Plainlist| * {{code|unsigned short}} * {{code|unsigned short int}} }} | ''Short'' unsigned integer type. Contains at least the {{closed-closed|size=100%|{{val|0}}, {{val|65535}}}} range.<ref name="c99sizes" /> || ≥16 || <code>%hu</code> || {{bracket|0, {{mono|USHRT_MAX}}}} || {{n/a}} |- | {{Plainlist| * {{code|int}} * {{code|signed}} * {{code|signed int}} }} | Basic signed integer type. Capable of containing at least the {{closed-closed|size=100%|{{val|−32767}}, {{val|+32767}}}} range.<ref name="c99sizes" />{{efn|name=restr}} || ≥16 || <code>%i</code> or <code>%d</code> || {{bracket|{{mono|INT_MIN}}, {{mono|INT_MAX}}}} || {{CNone|none<ref name=c99intconst>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 56, § 6.4.4.1 ''Integer constants''}}</ref>}} |- | {{Plainlist| * {{code|unsigned}} * {{code|unsigned int}} }} | Basic unsigned integer type. Contains at least the {{closed-closed|size=100%|{{val|0}}, {{val|65535}}}} range.<ref name="c99sizes" /> || ≥16 || <code>%u</code> || {{bracket|0, {{mono|UINT_MAX}}}} || {{mono|u}} or {{mono|U}}<ref name=c99intconst/> |- | {{Plainlist| * {{code|long}} * {{code|long int}} * {{code|signed long}} * {{code|signed long int}} }} | ''Long'' signed integer type. Capable of containing at least the {{closed-closed|size=100%|{{val|−2147483647}}, {{val|+2147483647}}}} range.<ref name="c99sizes" />{{efn|name=restr}} || ≥32 || <code>%li</code> or <code>%ld</code> || {{bracket|{{mono|LONG_MIN}}, {{mono|LONG_MAX}}}} || {{mono|l}} or {{mono|L}}<ref name=c99intconst/> |- | {{Plainlist| * {{code|unsigned long}} * {{code|unsigned long int}} }} | ''Long'' unsigned integer type. Capable of containing at least the {{closed-closed|size=100%|{{val|0}}, {{val|4294967295}}}} range.<ref name="c99sizes" /> || ≥32 || <code>%lu</code> || {{bracket|0, {{mono|ULONG_MAX}}}} || both {{mono|u}} or {{mono|U}} and {{mono|l}} or {{mono|L}}<ref name=c99intconst/> |- | {{Plainlist| * {{code|long long}} * {{code|long long int}} * {{code|signed long long}} * {{code|signed long long int}} }} | ''Long long'' signed integer type. Capable of containing at least the {{closed-closed|size=100%|{{val|−9223372036854775807}}, {{val|+9223372036854775807}}}} range.<ref name="c99sizes" />{{efn|name=restr}} Specified since the C99 version of the standard. || ≥64 || <code>%lli</code> or <code>%lld</code> || {{bracket|{{mono|LLONG_MIN}}, {{mono|LLONG_MAX}}}} || {{mono|ll}} or {{mono|LL}}<ref name=c99intconst/> |- | {{Plainlist| * {{code|unsigned long long}} * {{nowrap|{{code|unsigned long long int}}}} }} | ''Long long'' unsigned integer type. Contains at least the {{closed-closed|size=100%|{{val|0}}, {{val|18446744073709551615}}}} range.<ref name="c99sizes" /> Specified since the C99 version of the standard. || ≥64 || <code>%llu</code> || {{bracket|0, {{mono|ULLONG_MAX}}}} || both {{mono|u}} or {{mono|U}} and {{mono|ll}} or {{mono|LL}}<ref name=c99intconst/> |- | {{code|float}} || Real floating-point type, usually referred to as a single-precision floating-point type. Actual properties unspecified (except minimum limits); however, on most systems, this is the IEEE 754 single-precision binary floating-point format (32 bits). This format is required by the optional Annex F "IEC 60559 floating-point arithmetic". | | Converting from text:{{efn|These format strings also exist for formatting to text, but operate on a double.}}{{ubl | <code>%f</code> <code>%F</code> | <code>%g</code> <code>%G</code> | <code>%e</code> <code>%E</code> | <code>%a</code> <code>%A</code> }} || || {{mono|f}} or {{mono|F}} |- | {{code|double}} || Real floating-point type, usually referred to as a double-precision floating-point type. Actual properties unspecified (except minimum limits); however, on most systems, this is the IEEE 754 double-precision binary floating-point format (64 bits). This format is required by the optional Annex F "IEC 60559 floating-point arithmetic". | | {{ubl | <code>%lf</code> <code>%lF</code> | <code>%lg</code> <code>%lG</code> | <code>%le</code> <code>%lE</code> | <code>%la</code> <code>%lA</code>{{efn|name=uplowcase|Uppercase differs from lowercase in the output. Uppercase specifiers produce values in the uppercase, and lowercase in lower (%A, %E, %F, %G produce such values as INF, NAN and E (exponent) in uppercase)}} }} || ||none |- | {{code|long double}} || Real floating-point type, usually mapped to an extended precision floating-point number format. Actual properties unspecified. It can be either x86 extended-precision floating-point format (80 bits, but typically 96 bits or 128 bits in memory with padding bytes), the non-IEEE "double-double" (128 bits), IEEE 754 quadruple-precision floating-point format (128 bits), or the same as double. See the article on long double for details. | | <code>%Lf</code> <code>%LF</code><br /><code>%Lg</code> <code>%LG</code><br /><code>%Le</code> <code>%LE</code><br /><code>%La</code> <code>%LA</code>{{efn|name=uplowcase}} || || {{mono|l}} or {{mono|L}} |} {{noteslist}}
The actual size of the integer types varies by implementation. The standard requires only size relations between the data types and minimum sizes for each data type:
The relation requirements are that the {{code|long long}} is not smaller than {{code|long}}, which is not smaller than {{code|int}}, which is not smaller than {{code|short}}. As {{code|char}}'s size is always the minimum supported data type, no other data types (except bit-fields) can be smaller.
The minimum size for {{code|char}} is 8 bits, the minimum size for {{code|short}} and {{code|int}} is 16 bits, for {{code|long}} it is 32 bits and {{code|long long}} must contain at least 64 bits.
The type {{code|int}} should be the integer type that the target processor is most efficiently working with. This allows great flexibility: for example, all types can be 64-bit. However, several different integer width schemes (data models) are popular. Because the data model defines how different programs communicate, a uniform data model is used within a given operating system application interface.<ref>{{cite web|url=http://www.unix.org/version2/whatsnew/lp64_wp.html | title=64-Bit Programming Models: Why LP64? |publisher=The Open Group | access-date=9 November 2011}}</ref>
In practice, {{code|char}} is usually 8 bits in size and {{code|short}} is usually 16 bits in size (as are their unsigned counterparts). This holds true for platforms as diverse as 1990s SunOS 4 Unix, Microsoft MS-DOS, modern Linux, and Microchip MCC18 for embedded 8-bit PIC microcontrollers. POSIX requires {{code|char}} to be exactly 8 bits in size.<ref>{{Cite web |title=Width of Type (The GNU C Library) |url=https://www.gnu.org/software/libc/manual/html_node/Width-of-Type.html |access-date=2022-07-30 |website=www.gnu.org}}</ref><ref>{{Cite web |title=<limits.h> |url=https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html |access-date=2022-07-30 |website=pubs.opengroup.org}}</ref>
Various rules in the C standard make {{code|unsigned char}} the basic type used for arrays suitable to store arbitrary non-bit-field objects: its lack of padding bits and trap representations, the definition of ''object representation'',<ref name=c99generalrepr/> and the possibility of aliasing.<ref>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 67, § 6.5 ''Expressions''}}</ref>
The actual size and behavior of floating-point types also vary by implementation. The only requirement is that {{code|long double}} is not smaller than {{code|double}}, which is not smaller than {{code|float}}. Usually, the 32-bit and 64-bit IEEE 754 binary floating-point formats are used for {{code|float}} and {{code|double}} respectively.
The C99 standard includes new real floating-point types {{code|float_t}} and {{code|double_t}}, defined in <code><math.h></code>. They correspond to the types used for the intermediate results of floating-point expressions when {{code|FLT_EVAL_METHOD}} is 0, 1, or 2. These types may be wider than {{code|long double}}.
C99 also added complex types: {{code|float _Complex}}, {{code|double _Complex}}, {{code|long double _Complex}}. C11 added imaginary types (which were described in an informative annex of C99): {{code|float _Imaginary}}, {{code|double _Imaginary}}, {{code|long double _Imaginary}}. Including the header <code><complex.h></code> allows all these types to be accessed with using {{code|complex}} and {{code|imaginary}} respectively.
==={{anchor|stdbool.h}}Boolean type=== C99 added a Boolean data type {{code|_Bool}}. Additionally, the <code><stdbool.h></code> header defines {{code|bool}} as a convenient alias for this type, and also provides macros for <code>true</code> and <code>false</code>. {{code|_Bool}} functions similarly to a normal integer type, with one exception: any conversion to a {{code|_Bool}} gives 0 (false) if the value equals 0; otherwise, it gives 1 (true). This behavior exists to avoid integer overflows in implicit narrowing conversions. For example, in the following code:
<syntaxhighlight lang=C> unsigned char b = 256;
if (b) { // do something } </syntaxhighlight>
Variable <code>b</code> evaluates to false if {{code|unsigned char}} has a size of 8 bits. This is because the value 256 does not fit in the data type, which results in the lower 8 bits of it being used, resulting in a zero value. However, changing the type causes the previous code to behave normally:
<syntaxhighlight lang=C> _Bool b = 256;
if (b) { // do something } </syntaxhighlight>
The type {{code|_Bool}} also ensures true values always compare equal to each other:
<syntaxhighlight lang=C> _Bool a = 1; _Bool b = 2;
if (a == b) { // this code will run } </syntaxhighlight>
In C23, {{code|bool}} (and its values <code>true</code> and <code>false</code>) became a core functionality of the language (making the contents of <code><stdbool.h></code> obsolescent<ref>{{Cite web|title=<stdbool.h>|url=https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/stdbool.h.html|website=pubs.opengroup.org|author=The IEEE and The Open Group|publisher=IEEE Std 1003.1-2024|date=2024}}</ref><ref>{{Cite web|title=Standard library header <stdbool.h>|url=https://en.cppreference.com/w/c/header/stdbool.html|publisher=cppreference.com|author=cppreference.com|website=cppreference.com|access-date=7 February 2026}}</ref>), allowing for the following examples of code:
<syntaxhighlight lang=C> bool b = true;
if (b) { // this code will run } </syntaxhighlight>
===Bit-precise integer types=== Since C23, the language allows the programmer to define integers that have a width of an arbitrary number of bits. Those types are specified as {{code|_BitInt(N)|lang=c}}, where ''N'' is an integer constant expression that denotes the number of bits, including the sign bit for signed types, represented in two's complement. The maximum value of ''N'' is provided by <code>BITINT_MAXWIDTH</code> and is at least <code>ULLONG_WIDTH</code>. Therefore, the type {{code|_BitInt(2)|lang=c}} (or {{code|signed _BitInt(2)|lang=c}}) takes values from −2 to 1 while {{code|unsigned _BitInt(2)|lang=c}} takes values from 0 to 3. The type {{code|unsigned _BitInt(1)|lang=c}} also exists, being either 0 or 1 and has no equivalent signed type.<ref name=c23>{{cite book | url=https://open-std.org/JTC1/SC22/WG14/www/docs/n3096.pdf| title=ISO/IEC 9899:2023 specification draft| at=p. 37, § 6.2.5 ''Types''}}</ref> A proposal for C2Y proposes to lift this restriction and allow {{code|signed _BitInt(1)|lang=c}} which then has the possible values 0 and -1, removing the special case for {{code|unsigned _BitInt(1)|lang=c}}.<ref name=n3699>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3699%2epdf| title=Integer Sets, v3, WG14 N 3699| at=p. 6, § 1.2 ''Bit-precise Integers''}}</ref>
==={{anchor|stddef.h}}Size and pointer difference types=== The C language specification includes the {{mono|typedef}}s {{code|size_t}} and {{code|ptrdiff_t}} to represent memory-related quantities. Their size is defined according to the target processor's arithmetic capabilities, not the memory capabilities, such as available address space. Both of these types are defined in the <code><stddef.h></code> header.
{{code|size_t}} is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. The operator {{mono|sizeof}} yields a value of the type {{code|size_t}}. The maximum size of {{code|size_t}} is provided via <code>SIZE_MAX</code>, a macro constant which is defined in the <code><stdint.h></code> header. {{code|size_t}} is guaranteed to be at least 16 bits wide. Additionally, POSIX includes {{code|ssize_t}}, which is a signed integer type of the same width as {{code|size_t}}.
{{code|ptrdiff_t}} is a signed integer type used to represent the difference between pointers. It is guaranteed to be valid only against pointers of the same type; subtraction of pointers consisting of different types is implementation-defined.
==={{anchor|limits.h|float.h}}Interface to the properties of the basic types=== Information about the actual properties, such as size, of the basic arithmetic types, is provided via macro constants in two headers: <code><limits.h></code> header defines macros for integer types and <code><float.h></code> header defines macros for floating-point types. The actual values depend on the implementation.
==== Properties of integer types ==== * <code>CHAR_BIT</code> – size of the char type in bits, commonly referred to as the size of a byte (at least 8 bits) * <code>SCHAR_MIN</code>, <code>SHRT_MIN</code>, <code>INT_MIN</code>, <code>LONG_MIN</code>, <code>LLONG_MIN</code><small>(C99)</small> – minimum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long * <code>SCHAR_MAX</code>, <code>SHRT_MAX</code>, <code>INT_MAX</code>, <code>LONG_MAX</code>, <code>LLONG_MAX</code><small>(C99)</small> – maximum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long * <code>UCHAR_MAX</code>, <code>USHRT_MAX</code>, <code>UINT_MAX</code>, <code>ULONG_MAX</code>, <code>ULLONG_MAX</code><small>(C99)</small> – maximum possible value of unsigned integer types: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long * <code>CHAR_MIN</code> – minimum possible value of char * <code>CHAR_MAX</code> – maximum possible value of char * <code>MB_LEN_MAX</code> – maximum number of bytes in a multibyte character * <code>BOOL_WIDTH</code> (C23) - bit width of <code>_Bool</code>, always 1 * <code>CHAR_WIDTH</code> (C23) - bit width of <code>char</code>; <code>CHAR_WIDTH</code>, <code>UCHAR_WIDTH</code> and <code>SCHAR_WIDTH</code> are equal to <code>CHAR_BIT</code> by definition * <code>SCHAR_WIDTH</code>, <code>SHRT_WIDTH</code>, <code>INT_WIDTH</code>, <code>LONG_WIDTH</code>, <code>LLONG_WIDTH</code> (C23) - bit width of <code>signed char</code>, <code>short</code>, <code>int</code>, <code>long</code>, and <code>long long</code> respectively * <code>UCHAR_WIDTH</code>, <code>USHRT_WIDTH</code>, <code>UINT_WIDTH</code>, <code>ULONG_WIDTH</code>, <code>ULLONG_WIDTH</code> (C23) - bit width of <code>unsigned char</code>, <code>unsigned short</code>, <code>unsigned int</code>, <code>unsigned long</code>, and <code>unsigned long long</code> respectively
==== Properties of floating-point types ==== * <code>FLT_MIN</code>, <code>DBL_MIN</code>, <code>LDBL_MIN</code> – minimum normalized positive value of float, double, long double respectively * <code>FLT_TRUE_MIN</code>, <code>DBL_TRUE_MIN</code>, <code>LDBL_TRUE_MIN</code> (C11) – minimum positive value of float, double, long double respectively * <code>FLT_MAX</code>, <code>DBL_MAX</code>, <code>LDBL_MAX</code> – maximum finite value of float, double, long double, respectively * <code>FLT_ROUNDS</code> – rounding mode for floating-point operations * <code>FLT_EVAL_METHOD</code> (C99) – evaluation method of expressions involving different floating-point types * <code>FLT_RADIX</code> – radix of the exponent in the floating-point types * <code>FLT_DIG</code>, <code>DBL_DIG</code>, <code>LDBL_DIG</code> – number of decimal digits that can be represented without losing precision by float, double, long double, respectively * <code>FLT_EPSILON</code>, <code>DBL_EPSILON</code>, <code>LDBL_EPSILON</code> – difference between 1.0 and the next representable value of float, double, long double, respectively * <code>FLT_MANT_DIG</code>, <code>DBL_MANT_DIG</code>, <code>LDBL_MANT_DIG</code> – number of <code>FLT_RADIX</code>-base digits in the floating-point significand for types float, double, long double, respectively * <code>FLT_MIN_EXP</code>, <code>DBL_MIN_EXP</code>, <code>LDBL_MIN_EXP</code> – minimum negative integer such that <code>FLT_RADIX</code> raised to a power one less than that number is a normalized float, double, long double, respectively * <code>FLT_MIN_10_EXP</code>, <code>DBL_MIN_10_EXP</code>, <code>LDBL_MIN_10_EXP</code> – minimum negative integer such that 10 raised to that power is a normalized float, double, long double, respectively * <code>FLT_MAX_EXP</code>, <code>DBL_MAX_EXP</code>, <code>LDBL_MAX_EXP</code> – maximum positive integer such that <code>FLT_RADIX</code> raised to a power one less than that number is a normalized float, double, long double, respectively * <code>FLT_MAX_10_EXP</code>, <code>DBL_MAX_10_EXP</code>, <code>LDBL_MAX_10_EXP</code> – maximum positive integer such that 10 raised to that power is a normalized float, double, long double, respectively * <code>DECIMAL_DIG</code> (C99) – minimum number of decimal digits such that any number of the widest supported floating-point type can be represented in decimal with a precision of <code>DECIMAL_DIG</code> digits and read back in the original floating-point type without changing its value. <code>DECIMAL_DIG</code> is at least 10.
=={{anchor|stdint.h|inttypes.h}}Fixed-width integer types== The C99 standard includes definitions of several new integer types to enhance the portability of programs.<ref name="c99"/> The already available basic integer types were deemed insufficient, because their actual sizes are implementation defined and may vary across different systems. The new types are especially useful in embedded environments where hardware usually supports only several types and that support varies between different environments. All new types are defined in <code><inttypes.h></code> header and also are available at <code><stdint.h></code> header. The types can be grouped into the following categories:
* Exact-width integer types that are guaranteed to have the same number ''n'' of bits across all implementations. Included only if it is available in the implementation. * Least-width integer types that are guaranteed to be the smallest type available in the implementation, that has at least specified number ''n'' of bits. Guaranteed to be specified for at least N=8,16,32,64. * Fastest integer types that are guaranteed to be the fastest integer type available in the implementation, that has at least specified number ''n'' of bits. Guaranteed to be specified for at least N=8,16,32,64. * Pointer integer types that are guaranteed to be able to hold a pointer. Included only if it is available in the implementation. * Maximum-width integer types that are guaranteed to be the largest integer type in the implementation.
The following table summarizes the types and the interface to acquire the implementation details (''n'' refers to the number of bits):
{| class=wikitable |- ! rowspan=2 | Type category ! colspan=3 | Signed types ! colspan=3 | Unsigned types |- ! Type ! Minimum value ! Maximum value ! Type ! Minimum value ! Maximum value |- ! Exact width | <code>int''n''_t</code> || <code>INT''n''_MIN</code> || <code>INT''n''_MAX</code> | <code>uint''n''_t</code> || 0 || <code>UINT''n''_MAX</code> |- ! Least width | <code>int_least''n''_t</code> || <code>INT_LEAST''n''_MIN</code> || <code>INT_LEAST''n''_MAX</code> | <code>uint_least''n''_t</code> || 0 || <code>UINT_LEAST''n''_MAX</code> |- ! Fastest | <code>int_fast''n''_t</code> || <code>INT_FAST''n''_MIN</code> || <code>INT_FAST''n''_MAX</code> | <code>uint_fast''n''_t</code> || 0 || <code>UINT_FAST''n''_MAX</code> |- ! Pointer | <code>intptr_t</code> || <code>INTPTR_MIN</code> || <code>INTPTR_MAX</code> | <code>uintptr_t</code> || 0 || <code>UINTPTR_MAX</code> |- ! Maximum width | <code>intmax_t</code> || <code>INTMAX_MIN</code> || <code>INTMAX_MAX</code> | <code>uintmax_t</code> || 0 || <code>UINTMAX_MAX</code> |}
===Printf and scanf format specifiers=== {{Main|printf format string |scanf format string}}
The <code><inttypes.h></code> header provides features that enhance the functionality of the types defined in the <code><stdint.h></code> header. It defines macros for printf format string and scanf format string specifiers corresponding to the types defined in <code><stdint.h></code> and several functions for working with the <code>intmax_t</code> and <code>uintmax_t</code> types. This header was added in C99.
===={{mono|printf}} format string====
The macros are in the format <code>PRI''{fmt}{type}''</code>. Here ''{fmt}'' defines the output formatting and is one of <code>d</code> (decimal), <code>x</code> (hexadecimal), <code>o</code> (octal), <code>u</code> (unsigned) and <code>i</code> (integer). ''{type}'' defines the type of the argument and is one of <code>''n''</code>, <code>FAST''n''</code>, <code>LEAST''n''</code>, <code>PTR</code>, <code>MAX</code>, where <code>''n''</code> corresponds to the number of bits in the argument.
===={{mono|scanf}} format string====
The macros are in the format <code>SCN''{fmt}{type}''</code>. Here ''{fmt}'' defines the output formatting and is one of <code>d</code> (decimal), <code>x</code> (hexadecimal), <code>o</code> (octal), <code>u</code> (unsigned) and <code>i</code> (integer). ''{type}'' defines the type of the argument and is one of <code>''n''</code>, <code>FAST''n''</code>, <code>LEAST''n''</code>, <code>PTR</code>, <code>MAX</code>, where <code>''n''</code> corresponds to the number of bits in the argument.
====Functions====
{{expand section|date=October 2011}}
==Additional floating-point types== Similarly to the fixed-width integer types, ISO/IEC TS 18661 specifies floating-point types for IEEE 754 interchange and extended formats in binary and decimal: * <code>_Float''N''</code> for binary interchange formats; * <code>_Decimal''N''</code> for decimal interchange formats; * <code>_Float''N''x</code> for binary extended formats; * <code>_Decimal''N''x</code> for decimal extended formats.
==Pointers== {{See also|Pointer (computer programming)#C pointers}}
Every data type <code>T</code> has a corresponding type ''pointer-to-<code>T</code>''. A pointer is a data type that contains the address of a storage location of a variable of a particular type. They are declared with the asterisk (<code>*</code>) type declarator following the basic storage type and preceding the variable name. Whitespace before or after the asterisk is optional. <syntaxhighlight lang=C> char *p; long *q; int *r; </syntaxhighlight>
Pointers may also be declared for pointer data types, thus creating multiple indirect pointers, such as {{code|char **}} and {{code|int ***}}, including pointers to array types. The latter are less common than an array of pointers, and their syntax may be confusing: <syntaxhighlight lang=C> char *pc[10]; // array of 10 elements of 'pointer to char' char (*pa)[10]; // pointer to a 10-element array of char </syntaxhighlight>
The element <code>pc</code> requires ten blocks of memory of the size of ''pointer-to-<code>char</code>'' (usually 40 or 80 bytes on common platforms), but element <code>pa</code> is only one pointer (size 4 or 8 bytes), and the data it refers to is an array of ten bytes ({{code|1=sizeof *pa == 10|2=c}}).
C also has a "pointer-to-<code>void</code>", <code>void *</code>.<ref name=c99pointers>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 47, § 6.3.2.3 ''Pointers''}}</ref> The name "pointer-to-<code>void</code>" does not imply it points to <code>void</code> memory (as <code>void</code> is an incomplete type with no size), but rather that it points to something of unspecified type. It must be converted to another pointer type before it can be dereferenced. A <code>void *</code> may not point to a function. A call to <code>malloc</code> returns <code>void *</code>, and <code>free</code> takes a <code>void *</code>.
C uses the concept of a null pointer to denote a pointer that does not refer to any valid data. The macro <code>NULL</code> is often used in place of a null pointer, relying on implicit type conversion when possible. However, this usage can be problematic and may be a source of programming errors. In particular, the expansion of <code>NULL</code> may have a pointer type or an integer type, depending on the implementation. C23 introduced the predefined constant <code>nullptr</code> and its type <code>nullptr_t</code> (which has the single value <code>nullptr</code>) to express a null pointer constant. <code>nullptr</code> is unambiguously a pointer, and may convert to any object or function pointer, and allows a specific <code>nullptr_t</code> case in <code>_Generic</code>.<ref>{{Cite web|title=nullptr_t|url=https://en.cppreference.com/w/c/types/nullptr_t.html|publisher=cppreference.com|author=cppreference.com|website=cppreference.com|access-date=7 February 2026}}</ref> The size and alignment of this type is the same as for a pointer to character type (or <code>void *</code>), but other pointer types may still have a different size and alignment; thus not all null pointers are replaceable with <code>nullptr</code>.<ref name="N3042">{{cite web |title=WR14-N3042: Introduce the nullptr constant |url=https://open-std.org/JTC1/SC22/WG14/www/docs/n3042.htm |website=open-std.org |archive-url=https://web.archive.org/web/20221224043228/https://open-std.org/JTC1/SC22/WG14/www/docs/n3042.htm |archive-date=December 24, 2022 |date=2022-07-22 |url-status=live}}</ref>
==Arrays== {{See also|Array (data type)}} For every type <code>T</code>, except void and function types, there exist the types ''"array of <code>N</code> elements of type <code>T</code>"''. An array is a collection of values, all of the same type, stored contiguously in memory. An array of size <code>N</code> is indexed by integers from <code>0</code> up to and including <code>''N'' − 1</code>. Here is a brief example:
<syntaxhighlight lang=C> int a[10]; // array of 10 elements, each of type int </syntaxhighlight>
Arrays can be initialized with a compound initializer, but not assigned. Arrays are passed to functions by passing a pointer to the first element. Multidimensional arrays are defined as ''"array of array ..."'', and all except the outermost dimension must have compile-time constant size:
<syntaxhighlight lang=C> int aa[10][8]; // array of 10 elements, each of type 'array of 8 int elements' </syntaxhighlight>
In C, a string is often stored as an array of <code>char</code> (<code>char[]</code>), but this is distinct from a pointer-to-<code>char</code> (<code>char *</code>). <code>char[]</code> cannot be reassigned, and lives where it is defined, while <code>char *</code> is reassignable, but cannot be modified. <syntaxhighlight lang="c"> char s[] = "Hello, world!"; char *p = s; // s decays to &s[0], p points to the first character </syntaxhighlight> Even if a function takes <code>T[]</code> as a parameter, it decays into a <code>T *</code> which points to its first element. <syntaxhighlight lang="c"> void f(int a[]);
// this is the same as: void f(int *a); </syntaxhighlight>
Indexing an array is defined in terms of pointer arithmetic, that is, <code>a[i]</code> is equivalent to <code>*(a + i)</code>.<ref name="Plauger1992">{{cite book |title=ANSI and ISO Standard C Programmer's Reference |last=Plauger |first=P J |author-link=P. J. Plauger |author2=Brodie, Jim |year=1992 |publisher=Microsoft Press |location=Redmond, WA |isbn=978-1-55615-359-4 |page=[https://archive.org/details/ansiisostandardc00plau/page/108 108] |url-access=registration |url=https://archive.org/details/ansiisostandardc00plau/}}</ref>
==Enums== {{main|Enumerated type}} In C, an enum is an integer type whose values are restricted to a set of named constants.<ref>{{Cite web|title=Enumerations|url=https://en.cppreference.com/w/c/language/enum.html|author=cppreference.com|publisher=cppreference.com|website=cppreference.com|access-date=7 February 2026}}</ref> Enums cannot be forward-declared. Enums may also directly be assigned values, and are commonly used with <code>switch</code> to enumerate multiple cases. <syntaxhighlight lang="c"> #include <stddef.h>
enum Status { OK = 200, NOT_FOUND = 404, SERVER_ERROR = 500 };
const char *get_status_string(enum Status status) { switch (status) { case OK: return "Success"; case NOT_FOUND: return "Not found"; case SERVER_ERROR: return "Server error"; default: unreachable(); } } </syntaxhighlight> Enum constants are known at compile time, and are often a safer means to define integral constants than macros. An enum's underlying size is usually <code>int</code>, but since C23 an enum's underlying size can be directly specified to be any integral type. <syntaxhighlight lang="c"> enum Color : char { RED = 1, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET }; </syntaxhighlight>
Because enums are not type-safe, they can be assigned a value of another enum. Enums also need not be assigned values necessarily within the range of defined values. <syntaxhighlight lang="c"> enum Color color = YELLOW; // YELLOW = 3 enum Month month = JUNE; // JUNE = 6
color = month; // color is now 6 (INDIGO) month = 999; // there may not be a value of 999 in enum Month, but still allowed </syntaxhighlight>
==Structs== {{Main|struct (C programming language)}} Structures (structs) aggregate the storage of multiple data items, of potentially differing data types, into one contiguous memory block referenced by a single variable. Members may possibly be padded for memory alignment, and thus it is often recommended to order fields from largest to smallest size for efficient memory usage.
<syntaxhighlight lang=C> struct Student { char name[50]; unsigned int id; unsigned int semester; float gpa; };
// Positional initialization - values matches field order struct Student alice = { "Alice", 123, 2, 3.8 }; // Designated initializer (since C99) struct Student bob = { .name = "Bob", .id = 246, .semester = 1, .gpa = 3.9 }; </syntaxhighlight>
Structs may also use bit fields to allow fields to share the same storage units, but layouts are implementation-defined. <syntaxhighlight lang="c"> struct Properties { // three fields can be compactly packed in one byte unsigned char visible : 1; // a occupies 1 bit unsigned char color : 3; // b occupies 3 bits unsigned char size : 4; // c occupies 4 bits }; </syntaxhighlight>
The memory layout of a struct is a language implementation issue for each platform, with a few restrictions. The memory address of the first member must be the same as the address of structure itself. Structs may be initialized or assigned to using compound literals. A function may directly return a struct, although this is often not efficient at run-time. Since C99, a struct may also end with a flexible array member.
Functions may take a struct as a parameter by value, but this is expensive as it copies the entire struct. Meanwhile, passing it by pointer is often preferable as the size of a pointer is known (typically 4 or 8 bytes). <syntaxhighlight lang="c"> #include <stdio.h>
// passing by value void print_student(struct Student s) { printf("Name: %s, ID: %d, in semester %d, with GPA: %.2f\n", s.name, s.id, s.semester, s.gpa); }
// passing by pointer void print_student(struct Student *s) { printf("Name: %s, ID: %d, in semester %d, with GPA: %.2f\n", s->name, s->id, s->semester, s->gpa); } </syntaxhighlight>
Structs can be composed of other structs: <syntaxhighlight lang="c"> struct Date { int year; int month; int day; };
struct Birthday { char name[50]; struct Date dob; }; </syntaxhighlight>
A struct containing a pointer to a struct of its own type is commonly used to build linked data structures: <syntaxhighlight lang=C> struct LinkedList { void *item; // stores the current item struct LinkedList *next; // stores the next list, or NULL if nothing next }; </syntaxhighlight>
==Unions== {{See also|Union type#C/C++}}
A union type is a special construct that permits access to the same memory block by using a choice of differing type descriptions. <syntaxhighlight lang="c"> // holds either an integer or floating point value union Number { int i; float f; } d;
d.i = 10; // d now holds 10 d.f = 3.14f; // d now holds 3.14, overwriting 10 </syntaxhighlight>
In the following example, a union of data types may be declared to permit reading the same data either as an integer, a float, or any other user declared type: <syntaxhighlight lang=C> union { int i; float f; struct { unsigned int u; double d; } s; } u; </syntaxhighlight> The total size of <code>u</code> is the size of <code>u.s</code> – which happens to be the sum of the sizes of <code>u.s.u</code> and <code>u.s.d</code> – since <code>s</code> is larger than both <code>i</code> and <code>f</code>. When assigning something to <code>u.i</code>, some parts of <code>u.f</code> may be preserved if <code>u.i</code> is smaller than <code>u.f</code>.
Reading from a union member is not the same as casting since the value of the member is not converted, but merely read.
==Function pointers== Function pointers allow referencing functions with a particular signature. For example, to store the address of the standard function <code>abs</code> in the variable <code>my_int_f</code>:
<syntaxhighlight lang=C> int (*my_int_f)(int) = &abs; // the & operator can be omitted, but makes clear that the "address of" abs is used here </syntaxhighlight>
Function pointers are invoked by name just like normal function calls. <syntaxhighlight lang="c"> #include <stdio.h> #include <stdlib.h>
int (*my_abs)(int) = &abs; int x = -42; int abs_of_x = my_abs(x); printf("abs(%d) = %d\n", x, abs_of_x); </syntaxhighlight>
==Type qualifiers== {{Main|Type qualifier}}
The aforementioned types can be characterized further by type qualifiers, yielding a ''qualified type''. {{as of|2014}} and C11, there are four type qualifiers in standard C: * <code>const</code> (C89) * <code>volatile</code> (C89) * <code>restrict</code> (C99) * <code>_Atomic</code> (C11){{snd}} the latter has a private name to avoid clashing with user names,<ref>[https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3631.pdf C11:The New C Standard], Thomas Plum</ref> but the more ordinary name <code>atomic</code> can be used if the <code><stdatomic.h></code> header is included. Of these, <code>const</code> is by far the best-known and most used{{citation needed|date=September 2015}}, appearing in the C standard library and encountered in any significant use of the C language, which must satisfy const-correctness. The other qualifiers are primarily used for low-level programming: <code>volatile</code> is intended suppress compiler optimisations on a variable by suggesting it may change at any time, <code>restrict</code> indicates that the object pointed to by a pointer is accessed only by that pointer, and <code>_Atomic</code> indicates that the object is "atomic", i.e. reads and writes are indivisible.
==See also== {{Wikibooks|C Programming|Variables}}
* C syntax * Uninitialized variable * Integer (computer science) * Offsetof
==References== {{Reflist}}
{{C programming language}} {{Data types}}
{{DEFAULTSORT:C Variable Types And Declarations}} Category:C (programming language) Category:C standard library Category:Data types Category:Articles with example C code