type
status
date
slug
summary
tags
category
icon
password
3.1 Variable Declarations
3.2 Arithmetic Types
3.2.1 Integral
In the table below,
n represents the CPU bit number. For example, in a 32-bit machine, the value of n is 32, and in a 64-bit machine, the value of n is 64.Native Type | Bytes | Range | Fixed width types <cstdint> |
bool | 1 | truefalse | ㅤ |
char | 1 | implementation defined | ㅤ |
signed char | 1 | [-128, 127] | int8_t |
unsigned char | 1 | [0, 255] | uint8_t |
short | 2 | [, ] | int16_t |
unsigned short | 2 | [, ] | uint16_t |
int | 4 | [, ] | int32_t |
unsigned int | 4 | [, ] | uint32_t |
long int | 4/8 | [, ] | int32_t/int64_t |
long unsigned int | 4/8 | [, ] | uint32_t/uint64_t |
long long int | 8 | [, ] | int64_t |
long long unsigned int | 8 | [, ] | uint64_t |
long unsigned int is 4 bytes on Windows64 systems3.2.2 Floating-Point
Native Type | IEEE | Bytes | Range | Fixed width types <cstdfloat> |
bfloat16 | N | 2 | [, ] | std::bfloat16_t |
float16 | Y | 2 | [, ] | std::float16_t |
float | Y | 4 | [, ] | std::float32_t |
double | Y | 8 | [, ] | std::float64_t |
3.2.3 Short Name
Signed Type | short name | Unsigned Type | short name |
signed char
| / | unsigned char
| / |
signed short int
| short
| unsigned short int
| unsigned short
|
signed int
| int
| unsigned int
| unsigned
|
signed long int
| long
| unsigned long int
| unsigned long
|
signed long long int
| long long
| unsigned long long int
| unsigned long long
|
3.2.4 Suffix(Literals)
Type | SUFFIX | Example | Notes |
int
| / | 2 | ㅤ |
unsigned int
| u, U | 3u | ㅤ |
long int
| l, L | 8L | ㅤ |
long unsigned
| ul, UL | 2ul | ㅤ |
long long int
| ll, LL | 4ll | ㅤ |
long long unsigned int
| ull, ULL | 7ULL | ㅤ |
float
| f, F | 3.0f | only decimal numbers |
double
| ㅤ | 3.0 | only decimal numbers |
C++23 Type | SUFFIX | Example | Notes |
std::bfloat16_t | bf16, BF16 | 3.0bf16 | only decimal numbers |
std::float16_t | f16, F16 | 3.0f16 | only decimal numbers |
std::float32_t | f32, F32 | 3.0f32 | only decimal numbers |
std::float64_t | f64, F64 | 3.0f64 | only decimal numbers |
std::float128_t | f128, F128 | 3.0f128 | only decimal numbers |
3.2.5 Prefix(Literals)
Representation | PREFIX | Example |
Binary C++14 | 0b | 0b010101 |
Octal | 0 | 0307 |
Hexadecimal | 0x/0X | 0xFFA010 |
C++14 also allows digit separators for improving the readability
1'000'0003.2.6 Other
- C++ also provides long double (no IEEE-754) of size 8/12/16 bytes depending on the implementation
- Reduced precision floating-point supports before C++23:
- Some compilers provide support for half (16-bit floating-point) (GCC for ARM:
__fp16, LLVM compiler: half ) - Some modern CPUs and GPUs provide half instructions - Software support: OpenGL, Photoshop, Lightroom, half.sourceforge.net
- C++ does not provide 128-bit integers even if some architectures support it. clang and gcc allow 128-bit integers as compiler extension (
__int128)
3.3 void Type
3.3.1 void
void is an incomplete type (not defined) without a valuevoidindicates also a function with no return type or no parameters e.g.void f(),f(void)
- In C
sizeof(void) == 1(GCC), while in C++ sizeof(void) does not compile!!
3.3.2 nullptr
C++11 introduces the new keyword nullptr to represent a null pointer (0x0) and replacing the NULL macro
Remember:
nullptr is not a pointer, but an object of type nullptr_t → safer3.4 Conversion Rules
Implicit type conversion rules, applied in order, before any operation:
: any operation (
*, +, /, -, %, etc.)(A) Floating point promotion
floating type integer type → floating type
(B) Implicit integer promotion
small integral type := any signed/unsigned integral type smaller than int
small integral type small integral type → int
(C) Size promotion
small type large type → large type
(D) Sign promotion
signed type unsigned type → unsigned type
Integers are not floating points!
Integral data types smaller than 32-bit are implicitly promoted to
int , independently if they are signed or unsignedUnary
+, -, ∼ and Binary +, -, &, etc. promotion:3.5 auto Declaration
C++11 The
auto keyword specifies that the type of the variable will be automatically deduced by the compiler (from its initializer)auto can be very useful for maintainability and for hiding complex type definitionsOn the other hand, it may make the code less readable if excessively used because of type hiding
Example:
auto x = 0; in general makes no sense (x is int)In C++11/C++14,
auto (as well as decltype ) can be used to define function output types
In C++20, auto can be also used to define function input
- 作者:KaiGuo
- 链接:https://blog.kaiguov5.com/article/5f5de253-c010-45e8-8177-949364ca2414
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。