Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Arithmetic types

      From cppreference.com
      <c‎ |language
       
       
       
       

      (See alsotype for type system overview andthe list of type-related utilities that are provided by the C library.)

      Contents

      Boolean type

      • _Bool (also accessible as the macrobool)(until C23)bool(since C23) — type, capable of holding one of the two values:1 and0 (also accessible as the macrostrue andfalse)(until C23)true andfalse(since C23).

      Note thatconversion to_Bool(until C23)bool(since C23) does not work the same as conversion to other integer types:(bool)0.5 evaluates totrue, whereas(int)0.5 evaluates to0.

      (since C99)

      [edit]Character types

      • signedchar — type for signed character representation.
      • unsignedchar — type for unsigned character representation. Also used to inspectobject representations (raw memory).
      • char — type for character representation. Equivalent to eithersignedchar orunsignedchar (which one is implementation-defined and may be controlled by a compiler commandline switch), butchar is a distinct type, different from bothsignedchar andunsignedchar.

      Note that the standard library also definestypedef nameswchar_t,char16_t andchar32_t(since C11) to represent wide charactersandchar8_t for UTF-8 characters(since C23).

      [edit]Integer types

      • shortint (also accessible asshort, may use the keywordsigned)
      • unsignedshortint (also accessible asunsignedshort)
      • int (also accessible assignedint)
      This is the most optimal integer type for the platform, and is guaranteed to be at least 16 bits. Most current systems use 32 bits (see Data models below).
      • unsignedint (also accessible asunsigned), the unsigned counterpart ofint, implementing modulo arithmetic. Suitable for bit manipulations.
      • longint (also accessible aslong)
      • unsignedlongint (also accessible asunsignedlong)
      • longlongint (also accessible aslonglong)
      • unsignedlonglongint (also accessible asunsignedlonglong)
      (since C99)
      • _BitInt(n) (also accessible assigned _BitInt(n)), the bit-precise signed integer types (wheren is replaced by an integer constant expression denoting the precise width (including the sign bit), that cannot be larger thanBITINT_MAXWIDTH from<limits.h>)
      • unsigned _BitInt(n), the bit-precise unsigned integer types (wheren is replaced by an integer constant expression denoting the precise width, that cannot be larger thanBITINT_MAXWIDTH from<limits.h>)
      (since C23)

      Note: as with all type specifiers, any order is permitted:unsignedlonglongint andlongintunsignedlong name the same type.

      The following table summarizes all available integer types and their properties:

      Type specifierEquivalent typeWidth in bits by data model
      C standard LP32 ILP32 LLP64 LP64
      char
      charat least
      8
      8888
      signedchar
      signedchar
      unsignedchar
      unsignedchar
      short
      shortintat least
      16
      16161616
      shortint
      signedshort
      signedshortint
      unsignedshort
      unsignedshortint
      unsignedshortint
      int
      intat least
      16
      16323232
      signed
      signedint
      unsigned
      unsignedint
      unsignedint
      long
      longintat least
      32
      32323264
      longint
      signedlong
      signedlongint
      unsignedlong
      unsignedlongint
      unsignedlongint
      longlong
      longlongint
      (C99)
      at least
      64
      64646464
      longlongint
      signedlonglong
      signedlonglongint
      unsignedlonglong
      unsignedlonglongint
      (C99)
      unsignedlonglongint

      Besides the minimal bit counts, the C Standard guarantees that

      1 ==sizeof(char)sizeof(short)sizeof(int)sizeof(long)sizeof(longlong).

      Note: this allows the extreme case in whichbyte are sized 64 bits, all types (includingchar) are 64 bits wide, andsizeof returns1 for every type.

      Note: integer arithmetic is defined differently for the signed and unsigned integer types. Seearithmetic operators, in particularinteger overflows.

      [edit]Data models

      The choices made by each implementation about the sizes of the fundamental types are collectively known asdata model. Four data models found wide acceptance:

      32 bit systems:

      • LP32 or2/4/4 (int is 16-bit,long and pointer are 32-bit)
      • Win16 API
      • ILP32 or4/4/4 (int,long, and pointer are 32-bit);
      • Win32 API
      • Unix and Unix-like systems (Linux, Mac OS X)

      64 bit systems:

      • LLP64 or4/4/8 (int andlong are 32-bit, pointer is 64-bit)
      • Win64 API
      • LP64 or4/8/8 (int is 32-bit,long and pointer are 64-bit)
      • Unix and Unix-like systems (Linux, Mac OS X)

      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 types

      C has threeor six(since C23) types for representing real floating-point values:

      • float — single precision floating-point type. MatchesIEEE-754 binary32 format if supported.
      • double — double precision floating-point type. MatchesIEEE-754binary64 format if supported.
      • longdouble — extended precision floating-point type. MatchesIEEE-754binary128 format if supported, otherwise matchesIEEE-754binary64-extended format if supported, otherwise matches some non-IEEE-754 extended floating-point format as long as its precision is better thanbinary64 and range is at least as good asbinary64, otherwise matches IEEE-754binary64 format.
        • binary128 format is used by some HP-UX, SPARC, MIPS, ARM64, and z/OS implementations.
        • The most well known IEEE-754binary64-extended format is 80-bit x87 extended precision format. It is used by many x86 and x86-64 implementations (a notable exception is MSVC, which implementslongdouble in the same format asdouble, i.e.binary64).
      If the implementation predefines the macro constant__STDC_IEC_60559_DFP__, the following decimal floating-point types are also supported.
      Otherwise, these decimal floating-point types are not supported.
      (since C23)

      Floating-point types may support special values:

      • infinity (positive and negative), seeINFINITY
      • thenegative zero,-0.0. It compares equal to the positive zero, but is meaningful in some arithmetic operations, e.g.1.0/0.0== INFINITY, but1.0/-0.0==-INFINITY)
      • not-a-number (NaN), which does not compare equal with anything (including itself). Multiple bit patterns represent NaNs, seenan,NAN. Note that C takes no special notice of signaling NaNs (specified by IEEE-754), and treats all NaNs as quiet.

      Real floating-point numbers may be used witharithmetic operators+-/* and various mathematical functions from<math.h>. Both built-in operators and library functions may raise floating-point exceptions and seterrno as described inmath_errhandling.

      Floating-point expressions may have greater range and precision than indicated by their types, seeFLT_EVAL_METHOD.Assignment,return, andcast force the range and precision to the one associated with the declared type.

      Floating-point expressions may also becontracted, 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 ofthe floating-point environment (most notably, the rounding direction).

      Implicit conversions are defined between real floating types and integer, complex, and imaginary types.

      SeeLimits of floating-point types and the<math.h> library for additional details, limits, and properties of the floating-point types.

      Complex floating types

      Complex floating types model the mathematicalcomplex 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:longdoublecomplex,complexlongdouble, and evendoublecomplexlong name the same type.

      Run this code
      #include <complex.h>#include <stdio.h> int main(void){doublecomplex z=1+2*I;    z=1/ z;printf("1/(1.0+2.0i) = %.1f%+.1fi\n",creal(z),cimag(z));}

      Output:

      1/(1.0+2.0i) = 0.2-0.4i

      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.

      (since C11)

      Each complex type has the sameobject representation andalignment requirements as anarray of two elements of the corresponding real type (float forfloatcomplex,double fordoublecomplex,longdouble forlongdoublecomplex). 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};floatcomplex z1, z2;memcpy(&z1, a,sizeof z1);// z1 becomes 1.0 + 2.0imemcpy(&z2, a+2,sizeof z2);// z2 becomes 3.0 + 4.0i

      Complex numbers may be used witharithmetic 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 seterrno as described inmath_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").

      This section is incomplete
      Reason: review other ops, link library
      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 providescproj to map all infinities to the canonical one (seearithmetic operators for the exact rules).

      Run this code
      #include <complex.h>#include <math.h>#include <stdio.h> int main(void){doublecomplex 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 infinityprintf("%f%+f*i\n",creal(z),cimag(z)); //  textbook formula would give//  cexp(∞+iNaN) ⇒ exp(∞)×(cis(NaN)) ⇒ NaN + I*NaN//  but C gives  ±∞+i*nandoublecomplex y=cexp(INFINITY+ I*NAN);printf("%f%+f*i\n",creal(y),cimag(y));}

      Possible output:

      inf+inf*i inf+nan*i

      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.

      This section is incomplete
      Reason: wording

      Imaginary floating types

      Imaginary floating types model the mathematicalimaginary numbers, that is numbers that can be written as a real number multiplied by the imaginary unit:biThe three imaginary types are

      Note: as with all type specifiers, any order is permitted:longdoubleimaginary,imaginarylongdouble, and evendoubleimaginarylong name the same type.

      Run this code
      #include <complex.h>#include <stdio.h> int main(void){doubleimaginary z=3*I;    z=1/ z;printf("1/(3.0i) = %+.1fi\n",cimag(z));}

      Output:

      1/(3.0i) = -0.3i

      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.

      (until C11)

      Imaginary numbers are supported if__STDC_IEC_559_COMPLEX__(until C23)__STDC_IEC_60559_COMPLEX__(since C23) is defined.

      (since C11)

      Each of the three imaginary types has the sameobject representation andalignment requirement as itscorresponding real type (float forfloatimaginary,double fordoubleimaginary,longdouble forlongdoubleimaginary).

      Note: despite that, imaginary types are distinct andnot compatible with their corresponding real types, which prohibits aliasing.

      Imaginary numbers may be used witharithmetic 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 seterrno as described inmath_errhandling.

      Increment and decrement are not defined for imaginary types.

      This section is incomplete
      Reason: review other ops, link library
      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 notationx+ I*y (whereI is defined as_Imaginary_I). Without imaginary types, certain special complex values cannot be created naturally. For example, ifI is defined as_Complex_I, then writing0.0+ I*INFINITY gives NaN as the real part, andCMPLX(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 ascsqrt:1.0-0.0*I results in the positive zero imaginary component ifI is defined as_Complex_I and the negative zero imaginary part requires the use ofCMPLX orconj.

      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]Keywords

      [edit]Range of values

      The 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 to127 for a signed 8-bit type), which corresponds to the limits ofone's complement orsign-and-magnitude.

      However, all popular data models (including all of ILP32, LP32, LP64, LLP64) and almost all C compilers usetwo'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 to127 for a signed 8-bit type).

      TypeSize in bitsFormatValue range
      ApproximateExact
      character8signed−128 to127
      unsigned0 to255
      16UTF-160 to65535
      32UTF-320 to1114111 (0x10ffff)
      integer16signed± 3.27 · 104−32768 to32767
      unsigned0 to6.55 · 1040 to65535
      32signed± 2.14 · 109−2,147,483,648 to2,147,483,647
      unsigned0 to4.29 · 1090 to4,294,967,295
      64signed± 9.22 · 1018−9,223,372,036,854,775,808 to9,223,372,036,854,775,807
      unsigned0 to1.84 · 10190 to18,446,744,073,709,551,615
      binary
      floating-
      point
      32IEEE-754
      • min subnormal:
        ± 1.401,298,4 · 10−45
      • min normal:
        ± 1.175,494,3 · 10−38
      • max:
        ± 3.402,823,4 · 1038
      • min subnormal:
        ±0x1p−149
      • min normal:
        ±0x1p−126
      • max:
        ±0x1.fffffep+127
      64IEEE-754
      • min subnormal:
        ± 4.940,656,458,412 · 10−324
      • min normal:
        ± 2.225,073,858,507,201,4 · 10−308
      • max:
        ± 1.797,693,134,862,315,7 · 10308
      • min subnormal:
        ±0x1p−1074
      • min normal:
        ±0x1p−1022
      • max:
        ±0x1.fffffffffffffp+1023
      80[note 1]x86
      • min subnormal:
        ± 3.645,199,531,882,474,602,528
         · 10−4951
      • min normal:
        ± 3.362,103,143,112,093,506,263
         · 10−4932
      • max:
        ± 1.189,731,495,357,231,765,021
         · 104932
      • min subnormal:
        ±0x1p−16445
      • min normal:
        ±0x1p−16382
      • max:
        ±0x1.fffffffffffffffep+16383
      128IEEE-754
      • min subnormal:
        ± 6.475,175,119,438,025,110,924,
        438,958,227,646,552,5 · 10−4966
      • min normal:
        ± 3.362,103,143,112,093,506,262,
        677,817,321,752,602,6 · 10−4932
      • max:
        ± 1.189,731,495,357,231,765,085,
        759,326,628,007,016,2 · 104932
      • min subnormal:
        ±0x1p−16494
      • min normal:
        ±0x1p−16382
      • max:
        ±0x1.ffffffffffffffffffffffffffff
        p+16383
      decimal
      floating-
      point
      32IEEE-754
      • min subnormal:
        ± 1 · 10-101
      • min normal:
        ± 1 · 10-95
      • max:
        ± 9.999'999 · 1096
      64IEEE-754
      • min subnormal:
        ± 1 · 10-398
      • min normal:
        ± 1 · 10-383
      • max:
        ± 9.999'999'999'999'999 · 10384
      128IEEE-754
      • min subnormal:
        ± 1 · 10-6176
      • min normal:
        ± 1 · 10-6143
      • max:
        ± 9.999'999'999'999'999'999'
        999'999'999'999'999 · 106144
      1. The object representation usually occupies 96/128 bits on 32/64-bit platforms respectively.

      Note: actual (as opposed to guaranteed minimal) ranges are available in the library headers<limits.h> and<float.h>.

      [edit]See also

      C++ documentation forFundamental types
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/arithmetic_types&oldid=180190"

      [8]ページ先頭

      ©2009-2025 Movatter.jp