이름공간
변수
행위

기본 유형

cppreference.com
< cpp‎ | language
 
 
C++ 언어
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications(deprecated)
noexcept specifier(C++11)
Exceptions
Namespaces
Types
decltype specifier(C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier(C++11)
auto specifier(C++11)
alignas specifier(C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration(C++11)
attributes(C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs(C++11)
Miscellaneous
Inline assembly
 

(See also type for type system overview and the list of type-related utilities that are provided by the C++ library)

목차

[편집]void형

void - 값 집합이 비어 있는 유형. It is an incomplete type that cannot be completed (consequently, objects of type void are disallowed). There are no arrays of void, nor references to void. However, pointers to void and functions returning type void (procedures in other languages) are permitted.

[편집]std::nullptr_t

<cstddef> 에 정의되어 있음.
typedef decltype(nullptr) nullptr_t;
(since C++11)

std::nullptr_t는 널 포인터 리터럴 nullptr의 유형이다. It is a distinct type that is not itself a pointer type or a pointer to member type. Its values are null pointer constant (see NULL), and may be implicitly converted to any pointer and pointer to member type.

sizeof(std::nullptr_t)sizeof(void*)와 같다.

[편집]자료 모형

The 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:

  • LP32 or 2/4/4 (16비트 int, 32비트 long, 32비트 포인터)
  • Win16 API
  • ILP32 or 4/4/4 (32비트 int, 32비트 long, 32비트 포인터);
  • Win32 API
  • Unix and Unix-like systems (Linux, macOS)

64 bit systems:

  • LLP64 or 4/4/8 (32비트 int, 32비트 long, 64비트 포인터)
  • Win64 API
  • LP64 or 4/8/8 (32비트 int, 64비트 long, 64비트 포인터)
  • Unix and Unix-like systems (Linux, macOS)

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).

[편집]부호 있는 정수형, 부호 없는 정수형

int - 기본적인 정수형이다. 키워드 int는 수식어가 있을 때 생략할 수 있다. 길이 수식어가 없다면, 적어도 16비트의 할당을 보장한다. 하지만 대부분의 32/64비트 시스템에서는 32비트 길이를 할당한다.

[편집]수식어

기본 정수형을 수식한다. 어떤 순서로든 사용할 수 있다. 각 모임에서 하나만이 유형 이름에 나타날 수 있다.

부호

signed - target type will have signed representation (this is the default if omitted)
unsigned - target type will have unsigned representation

크기

short - target type will be optimized for space and will have width of at least 16 bits.
long - target type will have width of at least 32 bits.

longlong - target type will have width of at least 64 bits.
(since C++11)

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

[편집]성질

The following table summarizes all available integer types and their properties in various common data models:

Type specifier Equivalent type Width in bits by data model
C++ standard LP32 ILP32 LLP64 LP64
signedchar
signedchar at least
8
8888
unsignedchar
unsignedchar
short
shortint at least
16
16161616
shortint
signedshort
signedshortint
unsignedshort
unsignedshortint
unsignedshortint
int
int at least
16
16323232
signed
signedint
unsigned
unsignedint
unsignedint
long
longint at least
32
32323264
longint
signedlong
signedlongint
unsignedlong
unsignedlongint
unsignedlongint
longlong
longlongint
(C++11)
at least
64
64646464
longlongint
signedlonglong
signedlonglongint
unsignedlonglong
unsignedlonglongint
(C++11)
unsignedlonglongint

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

std::size_t is the unsigned integer type of the result of the sizeof operator as well as the sizeof... operator and the alignof operator(since C++11).

See also Fixed width integer types.

(since C++11)

[편집]불형

bool - true 혹은 false 중 한 가지를 값으로 가지는 유형이다. sizeof(bool) 값은 구현에서 정의하고, 1과 다를 수 있다.

[편집]문자형

signedchar - signed character를 표현하는 유형이다.
unsignedchar - unsigned character를 표현하기 위한 유형이다. Also used to inspect object representations (raw memory).
char - 문자를 표현하는 유형으로, 현재 실행되고 있는 시스템에서 가장 효율적으로 사용될 수 있다. signedchar또는 unsignedchar 중 하나와 같은 표현과 배치를 갖지만, 다른 유형이다. Multibyte characters strings use this type to represent code units. For every value of type unsignedchar in range [0, 255], converting the value to char and then back to unsignedchar produces the original value.(since C++11) The signedness of char depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed.
wchar_t - type for wide character representation (see wide strings). It has the same size, signedness, and alignment as one of the integer types, but is a distinct type. In practice, it is 32 bits and holds UTF-32 on Linux and many other non-Windows systems, but 16 bits and holds UTF-16 code units on Windows. The standard used to require wchar_t to be large enough to represent any supported character code point. However, such requirement cannot be fulfilled on Windows, and thus it is considered as a defect and removed.

char16_t - UTF-16 문자를 표현하는 유형으로, 모든 UTF-16 코드 유닛(16비트)을 표현할 수 있도록 커야 한다. std::uint_least16_t와 같은 크기, 부호, 배치를 갖지만, 다른 유형이다.

char32_t - UTF-32 문자를 표현하는 유형으로, 모든 UTF-32 코드 유닛(32비트)을 표현할 수 있도록 커야 한다. std::uint_least32_t와 같은 크기, 부호, 배치를 갖지만, 다른 유형이다.
(since C++11)

char8_t - type for UTF-8 character representation, required to be large enough to represent any UTF-8 code unit (8 bits). It has the same size, signedness, and alignment as unsignedchar (and therefore, the same size and alignment as char and signedchar), but is a distinct type.
(since C++20)

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 which bytes are sized 64 bits, all types (including char) are 64 bits wide, and sizeof returns 1 for every type.

[편집]부동소수점형

The following three types and their cv-qualified versions are collectively called floating-point types.

float - single precision floating-point type. Matches IEEE-754 binary32 format if supported.
double - double precision floating-point type. Matches IEEE-754 binary64 format if supported.
longdouble - extended precision floating-point type. Matches IEEE-754 binary128 format if supported, otherwise matches IEEE-754 binary64-extended format if supported, otherwise matches some non-IEEE-754 extended floating-point format as long as its precision is better than binary64 and range is at least as good as binary64, otherwise matches IEEE-754 binary64 format.
  • binary128 format is used by some HP-UX, SPARC, MIPS, ARM64, and z/OS implementations.
  • The most well known IEEE-754 binary64-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 implements longdouble in the same format as double, i.e. binary64).

[편집]Properties

Floating-point types may support special values:

  • infinity (positive and negative), see INFINITY
  • the negative zero, -0.0. It compares equal to the positive zero, but is meaningful in some arithmetic operations, e.g. 1.0/0.0==INFINITY, but 1.0/-0.0==-INFINITY), and for some mathematical functions, e.g. sqrt(std::complex)
  • not-a-number (NaN), which does not compare equal with anything (including itself). Multiple bit patterns represent NaNs, see std::nan, NAN. Note that C++ takes no special notice of signalling NaNs other than detecting their support by std::numeric_limits::has_signaling_NaN, and treats all NaNs as quiet.

Real floating-point numbers may be used with arithmetic operators + - / * and various mathematical functions from <cmath>. 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. 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. Standard C++ does not restrict the accuracy of floating-point operations.

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 types.

See Limits of floating-point types and std::numeric_limits for additional details, limits, and properties of the floating-point types.

[편집]Range of values

The following table provides a reference for the limits of common numeric representations.

Prior to C++20, the C++ Standard allowed any signed integer representation, and the minimum guaranteed range of N-bit signed integers was from -(2N-1
-1)
to +2N-1
-1
(e.g. -127 to 127 for a signed 8-bit type), which corresponds to the limits of ones' complement or sign-and-magnitude.

However, all C++ compilers use two's complement representation, and as of C++20, it is the only representation allowed by the standard, with the guaranteed range from -2N-1
to +2N-1
-1
(e.g. -128 to 127 for a signed 8-bit type).

8-bit ones' complement and sign-and-magnitude representations for char have been disallowed since C++11 (via the resolution of 틀:cwg), because a UTF-8 code unit of value 0x80 used in a UTF-8 string literal must be storable in a char element object.

유형 크기 (비트) 형식 값 범위
근사값 정확한 값
문자 8 signed -128 to 127
unsigned 0 to 255
16 UTF-16 0 to 65535
32 UTF-32 0 to 1114111 (0x10ffff)
정수 16 signed ± 3.27 · 104-32768 to 32767
unsigned 0 to 6.55 · 1040 to 65535
32 signed ± 2.14 · 109-2,147,483,648 to 2,147,483,647
unsigned 0 to 4.29 · 1090 to 4,294,967,295
64 signed ± 9.22 · 1018-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned 0 to 1.84 · 10190 to 18,446,744,073,709,551,615
이진
부동 소수점
32 IEEE-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
64 IEEE-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-16446
  • min normal:
    ±0x1p-16382
  • max:
    ±0x1.fffffffffffffffep+16383
128 IEEE-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
  1. The object representation usually occupies 96/128 bits on 32/64-bit platforms respectively.

Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available in C numeric limits interface and std::numeric_limits.

[편집]Notes

틀:ftm begin틀:ftm틀:ftm틀:ftm틀:ftm end

[편집]Keywords

void, bool, true, false, char, wchar_t, char8_t, (since C++20)char16_t, char32_t, (since C++11)int, short, long, signed, unsigned, float, double

[편집]Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 238 C++98 the constraints placed on a floating-point implementation was unspecified specified as no constraint
CWG 1759 C++11 char is not guaranteed to be able to represent UTF-8 code unit 0x80 guaranteed
P2460R2 C++98 wchar_t was required to be able to represent distinct codes for all members
of the largest extended character set specified among the supported locales
not required

[편집]See also

C documentation for arithmetic types
close