Primary types
Main types
The C language provides the four basic arithmetic type specifiers , , and (as well as the boolean type ), and the modifiers , , , and . The following table lists the permissible combinations in specifying a large set of storage size-specific declarations. The actual size of the <math.h>
. They correspond to the types used for the intermediate results of floating-point expressions when is 0, 1, or 2. These types may be wider than .
C99 also added <complex.h>
allows all these types to be accessed with using and respectively.
Boolean type
C99 added a<stdbool.h>
header defines as a convenient alias for this type, and also provides macros for true
and false
. functions similarly to a normal integer type, with one exception: any assignments to a that are not 0 (false) are stored as 1 (true). This behavior exists to avoid b
evaluates to false if 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:
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 , 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 byBITINT_MAXWIDTH
and is at least ULLONG_WIDTH
. Therefore, the type (or ) takes values from −2 to 1 while takes values from 0 to 3. The type also exists, being either 0 or 1 and has no equivalent signed type.
Size and pointer difference types
The C language specification includes the s and 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
header (cstddef
in C++).
is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. The operator yields a value of the type . The maximum size of is provided via SIZE_MAX
, a macro constant which is defined in the < stdint.h>
header (cstdint
header in C++). is guaranteed to be at least 16 bits wide. Additionally, POSIX includes , which is a signed integer type of the same width as .
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.
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:
header (climits
header in C++) defines macros for integer types and
header (cfloat
header in C++) defines macros for floating-point types. The actual values depend on the implementation.
Properties of integer types
*CHAR_BIT
– size of the char type in bits, commonly referred to as the size of a SCHAR_MIN
, SHRT_MIN
, INT_MIN
, LONG_MIN
, LLONG_MIN
(C99) – minimum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long
* SCHAR_MAX
, SHRT_MAX
, INT_MAX
, LONG_MAX
, LLONG_MAX
(C99) – maximum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long
* UCHAR_MAX
, USHRT_MAX
, UINT_MAX
, ULONG_MAX
, ULLONG_MAX
(C99) – maximum possible value of unsigned integer types: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long
* CHAR_MIN
– minimum possible value of char
* CHAR_MAX
– maximum possible value of char
* MB_LEN_MAX
– maximum number of bytes in a multibyte character
* BOOL_WIDTH
(C23) - bit width of _Bool
, always 1
* CHAR_WIDTH
(C23) - bit width of char
; CHAR_WIDTH
, UCHAR_WIDTH
and SCHAR_WIDTH
are equal to CHAR_BIT
by definition
* SCHAR_WIDTH
, SHRT_WIDTH
, INT_WIDTH
, LONG_WIDTH
, LLONG_WIDTH
(C23) - bit width of signed char
, short
, int
, long
, and long long
respectively
* UCHAR_WIDTH
, USHRT_WIDTH
, UINT_WIDTH
, ULONG_WIDTH
, ULLONG_WIDTH
(C23) - bit width of unsigned char
, unsigned short
, unsigned int
, unsigned long
, and unsigned long long
respectively
Properties of floating-point types
*FLT_MIN
, DBL_MIN
, LDBL_MIN
– minimum normalized positive value of float, double, long double respectively
* FLT_TRUE_MIN
, DBL_TRUE_MIN
, LDBL_TRUE_MIN
(C11) – minimum positive value of float, double, long double respectively
* FLT_MAX
, DBL_MAX
, LDBL_MAX
– maximum finite value of float, double, long double, respectively
* FLT_ROUNDS
– rounding mode for floating-point operations
* FLT_EVAL_METHOD
(C99) – evaluation method of expressions involving different floating-point types
* FLT_RADIX
– radix of the exponent in the floating-point types
* FLT_DIG
, DBL_DIG
, LDBL_DIG
– number of decimal digits that can be represented without losing precision by float, double, long double, respectively
* FLT_EPSILON
, DBL_EPSILON
, LDBL_EPSILON
– difference between 1.0 and the next representable value of float, double, long double, respectively
* FLT_MANT_DIG
, DBL_MANT_DIG
, LDBL_MANT_DIG
– number of FLT_RADIX
-base digits in the floating-point significand for types float, double, long double, respectively
* FLT_MIN_EXP
, DBL_MIN_EXP
, LDBL_MIN_EXP
– minimum negative integer such that FLT_RADIX
raised to a power one less than that number is a normalized float, double, long double, respectively
* FLT_MIN_10_EXP
, DBL_MIN_10_EXP
, LDBL_MIN_10_EXP
– minimum negative integer such that 10 raised to that power is a normalized float, double, long double, respectively
* FLT_MAX_EXP
, DBL_MAX_EXP
, LDBL_MAX_EXP
– maximum positive integer such that FLT_RADIX
raised to a power one less than that number is a normalized float, double, long double, respectively
* FLT_MAX_10_EXP
, DBL_MAX_10_EXP
, LDBL_MAX_10_EXP
– maximum positive integer such that 10 raised to that power is a normalized float, double, long double, respectively
* DECIMAL_DIG
(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 DECIMAL_DIG
digits and read back in the original floating-point type without changing its value. DECIMAL_DIG
is at least 10.
Fixed-width integer types
The C99 standard includes definitions of several new integer types to enhance the portability of programs. 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
header (cinttypes
header in C++) and also are available at
header (cstdint
header in C++). 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):
Printf and scanf format specifiers
The
header (cinttypes
in C++) provides features that enhance the functionality of the types defined in the
header. It defines macros for printf format string and
and several functions for working with the intmax_t
and uintmax_t
types. This header was added in C99.
Printf format string
The macros are in the formatPRI'
. Here ' defines the output formatting and is one of d
(decimal), x
(hexadecimal), o
(octal), u
(unsigned) and i
(integer). ' defines the type of the argument and is one of ''n''
, FAST''n''
, LEAST''n''
, PTR
, MAX
, where ''n''
corresponds to the number of bits in the argument.
Scanf format string
The macros are in the formatSCN'
. Here ' defines the output formatting and is one of d
(decimal), x
(hexadecimal), o
(octal), u
(unsigned) and i
(integer). ' defines the type of the argument and is one of ''n''
, FAST''n''
, LEAST''n''
, PTR
, MAX
, where ''n''
corresponds to the number of bits in the argument.
Functions
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: *_FloatN
for binary interchange formats;
* _DecimalN
for decimal interchange formats;
* _FloatNx
for binary extended formats;
* _DecimalNx
for decimal extended formats.
Structures
Structures aggregate the storage of multiple data items, of potentially differing data types, into one memory block referenced by a single variable. The following example declares the data typestruct birthday
which contains the name and birthday of a person. The structure definition is followed by a declaration of the variable John
that allocates the needed storage.
Arrays
For every typeT
, except void and function types, there exist the types ''"array of N
elements of type T
"''. An array is a collection of values, all of the same type, stored contiguously in memory. An array of size N
is indexed by integers from 0
up to and including ''N''−1
. Here is a brief example:
Pointers
Every data typeT
has a corresponding type ''pointer to T
''. A pointer (computer programming), 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 (*
) type declarator following the basic storage type and preceding the variable name. Whitespace before or after the asterisk is optional.
pc
requires ten blocks of memory of the size of ''pointer to char
'' (usually 40 or 80 bytes on common platforms), but element pa
is only one pointer (size 4 or 8 bytes), and the data it refers to is an array of ten bytes ().
Unions
A union type is a special construct that permits access to the same memory block by using a choice of differing type descriptions. For 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:u
is the size of u.s
– which happens to be the sum of the sizes of u.s.u
and u.s.d
– since s
is larger than both i
and f
. When assigning something to u.i
, some parts of u.f
may be preserved if u.i
is smaller than u.f
.
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
abs
in the variable my_int_f
:
Type qualifiers
The aforementioned types can be characterized further by type qualifiers, yielding a ''qualified type''. and C11, there are four type qualifiers in standard C: const
( C89), volatile
( C89), restrict
( C99) and _Atomic
( C11) the latter has a private name to avoid clashing with user names,atomic
can be used if the
header is included. Of these, const
is by far the best-known and most used, appearing in the See also
* C syntax *References
{{DEFAULTSORT:C Variable Types And Declarations C (programming language) C standard library Data types Articles with example C code