Fixed width integer types (since C++11)

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

Summary: in this tutorial, you will learn about C integer types and understand how the signed / unsigned, short / long qualifiers work.

Introduction to the C integer types

age = ;

To store integers internally, C uses a fixed number of bits (a sequence of 0 and 1). The number of bits also changes from one computer to another.

For example, most UNIX machines use 32 bits (4 bytes) to represent integers. So the range of the int numbers is from -232 (-2,147,483,648) to 231-1 (2,147,483,647).

However, some legacy PC use 16 bits to represent integers. Therefore, the range of the integers is from -32,768 to 32,767.

Short / Long qualifiers

C provides you with two qualifiers short and long that change the size of the integers. Typically, short is often 16 bits, and long is at least 32 bits.

The rule is that short is not longer than int, and int is not longer than long. However, this depends on whether the compilers will respect the rule or not.

Signed / unsigned integers

C provides two qualifiers called signed and unsigned that apply to any integer. Unsigned integers are always positive and zero. For example:

quanity = ;

profit = ;

Ranges of integers

C defines exactly the minimum storage size of each integer type, e.g., the short takes at least 2 byes, long takes at least 4 bytes.

Getting the sizes of integer types

Was this tutorial helpful ?

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable’s type and name, as you’ve already seen:


The implementation may define typedef names intN_t, int_fastN_t, int_leastN_t, uintN_t, uint_fastN_t, and uint_leastN_t when N is not 8, 16, 32 or 64. Typedef names of the form intN_t may only be defined if the implementation supports an integer type of that width with no padding. Thus, std::uint24_t denotes an unsigned integer type with a width of exactly 24 bits.

Each of the macros listed in below is defined if and only if the implementation defines the corresponding typedef name. The macros INTN_C and UINTN_C correspond to the typedef names int_leastN_t and uint_leastN_t, respectively.

Function macros for minimum-width integer constants

UINT64_C // expands to a literal of type uint_least64_t and value 0x123

Format macro constants

Each of the PRI macros listed here is defined if and only if the implementation defines the corresponding typedef name.

Format constants for the family of functions

Each of the SCN macros listed in here is defined if and only if the implementation defines the corresponding typedef name and has a suitable length modifier for the type.

The C99 standard suggests that C++ implementations should not define the above limit, constant, or format macros unless the macros , or (respectively) are defined before including the relevant C header (stdint.h or inttypes.h). This recommendation was not adopted by any C++ standard and was removed in C11. However, some implementations (such as glibc 2.17) try to apply this rule, and it may be necessary to define the __STDC macros; C++ compilers may try to work around this by automatically defining them in some circumstances.

std::int8_t may be and std::uint8_t may be , but neither can be regardless of its signedness (because is not considered a “signed integer type” or “unsigned integer type”).

Про анемометры:  Анемометр ручной электронный АРЭ (1-35 м/с) :: Гидрометприбор

Using Underscore Characters in Numeric Literals

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example. to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;

// Invalid: cannot put underscores
// adjacent to a decimal point
float pi1 = 3_.1415F;
// Invalid: cannot put underscores
// adjacent to a decimal point
float pi2 = 3._1415F;
// Invalid: cannot put underscores
// prior to an L suffix
long socialSecurityNumber1 = 999_99_9999_L;

// OK (decimal literal)
int x1 = 5_2;
// Invalid: cannot put underscores
// At the end of a literal
int x2 = 52_;
// OK (decimal literal)
int x3 = 5_______2;

// Invalid: cannot put underscores
// in the 0x radix prefix
int x4 = 0_x52;
// Invalid: cannot put underscores
// at the beginning of a number
int x5 = 0x_52;
// OK (hexadecimal literal)
int x6 = 0x5_2;
// Invalid: cannot put underscores
// at the end of a number
int x7 = 0x52_;

— type with an empty set of values. It is an incomplete type that cannot be completed (consequently, objects of type are disallowed). There are no arrays of , nor references to . However, pointers to and functions returning type (procedures in other languages) are permitted.

is the type of the null pointer literal, 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 ), and may be implicitly converted to any pointer and pointer to member type.

is equal to .

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 ( is 16-bit, and pointer are 32-bit)
Win16 API
ILP32 or 4/4/4 (, , and pointer are 32-bit);
Win32 API
Unix and Unix-like systems (Linux, macOS)

64 bit systems:

LLP64 or 4/4/8 ( and are 32-bit, pointer is 64-bit)
Win32 API (also called the Windows API) with compilation target 64-bit ARM (AArch64) or x86-64 (a.k.a. x64)
LP64 or 4/8/8 ( is 32-bit, and pointer are 64-bit)
Unix and Unix-like systems (Linux, macOS)

Other models are very rare. For example, ILP64 (8/8/8: , , and pointer are 64-bit) only appeared in some early 64-bit Unix systems (e.g. UNICOS on Cray).

Standard integer types

— basic integer type. The keyword may be omitted if any of the modifiers listed below are used. If no length modifiers are present, it’s guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits (see below).

Modifies the basic integer type. Can be mixed in any order. Only one of each group can be present in type name.

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

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

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

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

Extended integer types

The extended integer types are implementation-defined. Note that fixed width integer types are typically aliases of the standard integer types.

Про анемометры:  Какие датчики существуют в природе?

— type, capable of holding one of the two values: true or false. The value of is implementation defined and might differ from .

— type for signed character representation.
— type for unsigned character representation. Also used to inspect object representations (raw memory).
— type for character representation which can be most efficiently processed on the target system (has the same representation and alignment as either or , but is always a distinct type). Multibyte characters strings use this type to represent code units. The signedness of 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.
— 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 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.

Besides the minimal bit counts, the C++ Standard guarantees that

== ≤ ≤ ≤ ≤ .

Note: this allows the extreme case in which bytes are sized 64 bits, all types (including ) are 64 bits wide, and sizeof returns 1 for every type.

Standard floating-point types

— single precision floating-point type. Matches IEEE-754 binary32 format if supported.
— double precision floating-point type. Matches IEEE-754 binary64 format if supported.
— 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 x87 80-bit extended precision format. It is used by many x86 and x86-64 implementations (a notable exception is MSVC, which implements in the same format as , i.e. binary64).

Extended floating-point types

The extended floating-point types are implementation-defined. They may include fixed width floating-point types.

Floating-point types may support special values:

Floating-point expressions may have greater range and precision than indicated by their types, see . 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 for additional details, limits, and properties of the floating-point types.

Range of values

Prior to C++20, the C++ Standard allowed any signed integer representation, and the minimum guaranteed range of N-bit signed integers was from to (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 to (e.g. −128 to 127 for a signed 8-bit type).

8-bit ones’ complement and sign-and-magnitude representations for have been disallowed since C++11 (via the resolution of CWG issue 1759), because a UTF-8 code unit of value 0x80 used in a UTF-8 string literal must be storable in a type object.

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

Про анемометры:  Напольный газовый котел Жуковский АОГВ-23,2-3 Универсал (Н) | Интернет-магазин Master Water



Default Values

It’s not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

You may have noticed that the new keyword isn’t used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it’s possible to assign a literal to a variable of a primitive type:

boolean result = true;
char capitalC = ‘C’;
byte b = 100;
short s = 10000;
int i = 100000;

Integer Literals

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

Values of the integral types byte, short, int, and long can be created from int literals. Values of type long that exceed the range of int can be created from long literals. Integer literals can be expressed by these number systems:

// The number 26, in decimal
int decVal = 26;
// The number 26, in hexadecimal
int hexVal = 0x1a;
// The number 26, in binary
int binVal = 0b11010;

Floating-Point Literals

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1 = 123.4f;

Character and String Literals

Literals of types char and String may contain any Unicode (UTF-16) characters. If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a “Unicode escape” such as ‘Ĉ’ (capital C with circumflex), or “Sí Señor” (Sí Señor in Spanish). Always use ‘single quotes’ for char literals and “double quotes” for String literals. Unicode escape sequences may be used elsewhere in a program (such as in field names, for example), not just in char or String literals.

The Java programming language also supports a few special escape sequences for char and String literals: (backspace), (tab),
(line feed), (form feed),
(carriage return), ” (double quote), ‘ (single quote), and \ (backslash).

There’s also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There’s little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.

Finally, there’s also a special kind of literal called a class literal, formed by taking a type name and appending “.class”; for example, String.class. This refers to the object (of type Class) that represents the type itself.

Оцените статью
Добавить комментарий