C++ Crash Course: a fast-Paced Introduction



Yüklə 7 Mb.
Pdf görüntüsü
səhifə63/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   59   60   61   62   63   64   65   66   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

Integer Types
Integer types store whole numbers: those that you can write without a frac-
tional component. The four sizes of integer types are short intintlong int
and long long int. Each can be either signed or unsigned. A signed variable can 
be positive, negative, or zero, and an unsigned variable must be non-negative.
Integer types are signed and 
int
by default, which means you can use the 
following shorthand notations in your programs: 
short

long
, and 
long long
rather than 
short int

long int
, and 
long long int
. Table 2-1 lists all available 
C++ integer types, whether each is signed or unsigned, the size of each (in 
bytes) across platforms, as well as the format specifier for each.
Table 2-1: 
Integer Types, Sizes, and Format Specifiers
Type
Signed
Size in bytes
printf
format 
specifier
32-bit OS
64-bit OS
Windows
Linux/Mac Windows
Linux/Mac
short
Yes
2
2
2
2
%hd
unsigned short
No
2
2
2
2
%hu
int
Yes
4
4
4
4
%d
unsigned int
No
4
4
4
4
%u
long
Yes
4
4
4
8
%ld
unsigned long
No
4
4
4
8
%lu
long long
Yes
8
8
8
8
%lld
unsigned long long
No
8
8
8
8
%llu
Notice that the integer type sizes vary across platforms: 64-bit Windows 
and Linux/Mac have different sizes for a 
long
integer (4 and 8, respectively).
Usually, a compiler will warn you of a mismatch between format speci-
fier and integer type. But you must ensure that the format specifiers are cor-
rect when you’re using them in 
printf
statements. Format specifiers appear 
here so you can print integers to console in examples to follow.
N O T E
 
If you want to enforce guaranteed integer sizes, you can use integer types in the 

 library. For example, if you need a signed integer with exactly 8, 16, 32, 
or 64 bits, you could use 
int8_t

int16_t

int32_t
, or 
int64_t
. You’ll find options 
for the fastest, smallest, maximum, signed, and unsigned integer types to meet your 
requirements. But because this header is not always available in every platform
you should only use 
cstdint
 types when there is no other alternative.


Types

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   59   60   61   62   63   64   65   66   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin