(See also type for type system overview and the list of type-related utilities that are provided by the C library.)
Boolean typeNote that conversion to _Bool(until C23)bool(since C23) does not work the same as conversion to other integer types: (bool)0.5 evaluates to true, whereas (int)0.5 evaluates to â0â.
(since C99) [edit] Character typesNote that the standard library also defines typedef names wchar_t, char16_t and char32_t(since C11) to represent wide characters and char8_t for UTF-8 characters(since C23).
[edit] Integer typesn
is replaced by an integer constant expression denoting the precise width (including the sign bit), that cannot be larger than BITINT_MAXWIDTH from <limits.h>)n
is replaced by an integer constant expression denoting the precise width, that cannot be larger than BITINT_MAXWIDTH from <limits.h>)Note: as with all type specifiers, any order is permitted: unsigned long long int and long int unsigned long name the same type.
The following table summarizes all available integer types and their properties:
Type specifier Equivalent type Width in bits by data model C standard LP32 ILP32 LLP64 LP64char
char at leastsigned char
signed charunsigned char
unsigned charshort
short int at leastshort int
signed short
signed short int
unsigned short
unsigned short intunsigned short int
int
int at leastsigned
signed int
unsigned
unsigned intunsigned int
long
long int at leastlong int
signed long
signed long int
unsigned long
unsigned long intunsigned long int
long long
long long intlong long int
signed long long
signed long long int
unsigned long long
unsigned long long intunsigned long long int
Besides the minimal bit counts, the C Standard guarantees that
≤
sizeof(short) ≤
sizeof(int) ≤
sizeof(long) ≤
sizeof(long long).
Note: this allows the extreme case in which byte are sized 64 bits, all types (including char) are 64 bits wide, and sizeof returns 1 for every type.
Note: integer arithmetic is defined differently for the signed and unsigned integer types. See arithmetic operators, in particular integer overflows.
[edit] Data modelsThe choices made by each implementation about the sizes of the fundamental types are collectively known as data model. Four data models found wide acceptance:
32 bit systems:
64 bit systems:
Other models are very rare. For example, ILP64 (8/8/8: int, long, and pointer are 64-bit) only appeared in some early 64-bit Unix systems (e.g. Unicos on Cray).
Note that exact-width integer types are available in <stdint.h> since C99.
[edit] Real floating typesC has three or six(since C23) types for representing real floating-point values:
Floating-point types may support special values:
Real floating-point numbers may be used with arithmetic operators +
-
/
*
and various mathematical functions from <math.h>. Both built-in operators and library functions may raise floating-point exceptions and set errno as described in math_errhandling.
Floating-point expressions may have greater range and precision than indicated by their types, see FLT_EVAL_METHOD. Assignment, return, and cast force the range and precision to the one associated with the declared type.
Floating-point expressions may also be contracted, that is, calculated as if all intermediate values have infinite range and precision, see #pragma STDC FP_CONTRACT.
Some operations on floating-point numbers are affected by and modify the state of the floating-point environment (most notably, the rounding direction).
Implicit conversions are defined between real floating types and integer, complex, and imaginary types.
See Limits of floating-point types and the <math.h> library for additional details, limits, and properties of the floating-point types.
Complex floating typesComplex floating types model the mathematical complex number, that is the numbers that can be written as a sum of a real number and a real number multiplied by the imaginary unit: a + bi
The three complex types are
Note: as with all type specifiers, any order is permitted: long double complex, complex long double, and even double complex long name the same type.
#include <complex.h> #include <stdio.h> int main(void) { double complex z = 1 + 2*I; z = 1 / z; printf("1/(1.0+2.0i) = %.1f%+.1fi\n", creal(z), cimag(z)); }
Output:
If the macro constant __STDC_NO_COMPLEX__
is defined by the implementation, the complex types (as well as the library header <complex.h>) are not provided.
Each complex type has the same object representation and alignment requirements as an array of two elements of the corresponding real type (float for float complex, double for double complex, long double for long double complex). The first element of the array holds the real part, and the second element of the array holds the imaginary component.
float a[4] = {1, 2, 3, 4}; float complex z1, z2; memcpy(&z1, a, sizeof z1); // z1 becomes 1.0 + 2.0i memcpy(&z2, a+2, sizeof z2); // z2 becomes 3.0 + 4.0i
Complex numbers may be used with arithmetic operators +
-
*
and /
, possibly mixed with imaginary and real numbers. There are many mathematical functions defined for complex numbers in <complex.h>. Both built-in operators and library functions may raise floating-point exceptions and set errno as described in math_errhandling.
Increment and decrement are not defined for complex types.
Relational operators are not defined for complex types (there is no notion of "less than").
Implicit conversions are defined between complex types and other arithmetic types.In order to support the one-infinity model of complex number arithmetic, C regards any complex value with at least one infinite part as an infinity even if its other part is a NaN, guarantees that all operators and functions honor basic properties of infinities and provides cproj to map all infinities to the canonical one (see arithmetic operators for the exact rules).
#include <complex.h> #include <math.h> #include <stdio.h> int main(void) { double complex z = (1 + 0*I) * (INFINITY + I*INFINITY); // textbook formula would give // (1+i0)(â+iâ) â (1Ãâ â 0Ãâ) + i(0Ãâ+1Ãâ) â NaN + I*NaN // but C gives a complex infinity printf("%f%+f*i\n", creal(z), cimag(z)); // textbook formula would give // cexp(â+iNaN) â exp(â)Ã(cis(NaN)) â NaN + I*NaN // but C gives ±â+i*nan double complex y = cexp(INFINITY + I*NAN); printf("%f%+f*i\n", creal(y), cimag(y)); }
Possible output:
C also treats multiple infinities so as to preserve directional information where possible, despite the inherent limitations of the Cartesian representation:
multiplying the imaginary unit by real infinity gives the correctly-signed imaginary infinity: i à â = iâ. Also, i à (â â iâ) = â + iâ indicates the reasonable quadrant.
Imaginary floating typesImaginary floating types model the mathematical imaginary numbers, that is numbers that can be written as a real number multiplied by the imaginary unit: bi The three imaginary types are
Note: as with all type specifiers, any order is permitted: long double imaginary, imaginary long double, and even double imaginary long name the same type.
#include <complex.h> #include <stdio.h> int main(void) { double imaginary z = 3*I; z = 1 / z; printf("1/(3.0i) = %+.1fi\n", cimag(z)); }
Output:
An implementation that defines __STDC_IEC_559_COMPLEX__
is recommended, but not required to support imaginary numbers. POSIX recommends checking if the macro _Imaginary_I is defined to identify imaginary number support.
Imaginary numbers are supported if __STDC_IEC_559_COMPLEX__
(until C23)__STDC_IEC_60559_COMPLEX__
(since C23) is defined.
Each of the three imaginary types has the same object representation and alignment requirement as its corresponding real type (float for float imaginary, double for double imaginary, long double for long double imaginary).
Note: despite that, imaginary types are distinct and not compatible with their corresponding real types, which prohibits aliasing.
Imaginary numbers may be used with arithmetic operators +
-
*
and /
, possibly mixed with complex and real numbers. There are many mathematical functions defined for imaginary numbers in <complex.h>. Both built-in operators and library functions may raise floating-point exceptions and set errno as described in math_errhandling.
Increment and decrement are not defined for imaginary types.
Implicit conversions are defined between imaginary types and other arithmetic types.The imaginary numbers make it possible to express all complex numbers using the natural notation x + I*y (where I is defined as _Imaginary_I). Without imaginary types, certain special complex values cannot be created naturally. For example, if I is defined as _Complex_I, then writing 0.0 + I*INFINITY gives NaN as the real part, and CMPLX(0.0, INFINITY) must be used instead. Same goes for the numbers with the negative zero imaginary component, which are meaningful when working with the library functions with branch cuts, such as csqrt: 1.0 - 0.0*I results in the positive zero imaginary component if I is defined as _Complex_I and the negative zero imaginary part requires the use of CMPLX or conj.
Imaginary types also simplify implementations; multiplication of an imaginary by a complex can be implemented straightforwardly with two multiplications if the imaginary types are supported, instead of four multiplications and two additions.
(since C99) [edit] KeywordsThe following table provides a reference for the limits of common numeric representations.
Prior to C23, the C Standard allowed any signed integer representation, and the minimum guaranteed range of N-bit signed integers was from \(\scriptsize -(2^{N-1}-1)\)-(2N-1
-1) to \(\scriptsize +2^{N-1}-1\)+2N-1
-1 (e.g. -127 to 127 for a signed 8-bit type), which corresponds to the limits of one's complement or sign-and-magnitude.
However, all popular data models (including all of ILP32, LP32, LP64, LLP64) and almost all C compilers use two's complement representation (the only known exceptions are some compilers for UNISYS), and as of C23, it is the only representation allowed by the standard, with the guaranteed range from \(\scriptsize -2^{N-1}\)-2N-1
to \(\scriptsize +2^{N-1}-1\)+2N-1
-1 (e.g. -128 to 127 for a signed 8-bit type).
Note: actual (as opposed to guaranteed minimal) ranges are available in the library headers <limits.h> and <float.h>.
[edit] See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4