|
|
a
, b
, and result
, but we could have called the variables any names we could have come up with, as long as they were valid C++ identifiers._
). Spaces, punctuation marks, and symbols cannot be part of an identifier. In addition, identifiers shall always begin with a letter. They can also begin with an underline character (_
), but such identifiers are -on most cases- considered reserved for compiler-specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case can they begin with a digit.
alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast, struct, switch, template, this, thread_local, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq
RESULT
variable is not the same as the result
variable or the Result
variable. These are three different identifiers identifiying three different variables.'A'
or '$'
. The most basic type is char
, which is a one-byte character. Other types are also provided for wider characters.7
or 1024
. They exist in a variety of sizes, and can either be signed or unsigned, depending on whether they support negative values or not.3.14
or 0.01
, with different levels of precision, depending on which of the three floating-point types is used.bool
, can only represent one of two states, true
or false
.Group | Type names* | Notes on size / precision |
---|---|---|
Character types | char | Exactly one byte in size. At least 8 bits. |
char16_t | Not smaller than char . At least 16 bits. | |
char32_t | Not smaller than char16_t . At least 32 bits. | |
wchar_t | Can represent the largest supported character set. | |
Integer types (signed) | signed char | Same size as char . At least 8 bits. |
signed short int | Not smaller than char . At least 16 bits. | |
signed int | Not smaller than short . At least 16 bits. | |
signed long int | Not smaller than int . At least 32 bits. | |
signed long long int | Not smaller than long . At least 64 bits. | |
Integer types (unsigned) | unsigned char | (same size as their signed counterparts) |
unsigned short int | ||
unsigned int | ||
unsigned long int | ||
unsigned long long int | ||
Floating-point types | float | |
double | Precision not less than float | |
long double | Precision not less than double | |
Boolean type | bool | |
Void type | void | no storage |
Null pointer | decltype(nullptr) |
signed
and int
components - only the part not in italics is required to identify the type, the part in italics is optional. I.e., signed short int
can be abbreviated as signed short
, short int
, or simply short
; they all identify the same fundamental type.char
(which has a size of exactly one byte), none of the fundamental types has a standard size specified (but a minimum size, at most). Therefore, the type is not required (and in many cases is not) exactly this minimum size. This does not mean that these types are of an undetermined size, but that there is no standard size across all compilers and machines; each compiler implementation may specify the sizes for these types that fit the best the architecture where the program is going to run. This rather generic size specification for types gives the C++ language a lot of flexibility to be adapted to work optimally in all kinds of platforms, both present and future. Size | Unique representable values | Notes |
---|---|---|
8-bit | 256 | = 28 |
16-bit | 65 536 | = 216 |
32-bit | 4 294 967 296 | = 232 (~4 billion) |
64-bit | 18 446 744 073 709 551 616 | = 264 (~18 billion billion) |
char
, int
, and double
are typically selected to represent characters, integers, and floating-point values, respectively. The other types in their respective groups are only used in very particular cases.<limits>
). If for some reason, types of specific sizes are needed, the library defines certain fixed-size type aliases in header <cstdint>
.void
, which identifies the lack of type; and the type nullptr
, which is a special type of pointer. Both types will be discussed further in a coming chapter about pointers.
|
|
int
with the identifier a
. The second one declares a variable of type float
with the identifier mynumber
. Once declared, the variables a
and mynumber
can be used within the rest of their scope in the program.
|
|
a
, b
and c
), all of them of type int
, and has exactly the same meaning as:
|
|
|
|
4 |
type identifier = initial_value;
int
called x
and initialize it to a value of zero from the same moment it is declared, we can write:
|
|
()
):type identifier (initial_value);
|
|
{}
) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011):type identifier {initial_value};
|
|
|
|
6 |
auto
as the type specifier for the variable:
|
|
bar
is declared as having an auto
type; therefore, the type of bar
is the type of the value used to initialize it: in this case it uses the type of foo
, which is int
.decltype
specifier:
|
|
bar
is declared as having the same type as foo
.auto
and decltype
are powerful features recently added to the language. But the type deduction features they introduce are meant to be used either when the type cannot be obtained by other means or when using it improves code readability. The two examples above were likely neither of these use cases. In fact they probably decreased readability, since, when reading the code, one has to search for the type of foo
to actually know the type of bar
.string
class. Variables of this type are able to store sequences of characters, such as words or sentences. A very useful feature!<string>
):
|
|
This is a string |
|
|
|
|
This is the initial string content This is a different string content |
endl
manipulator ends the line (printing a newline character and flushing the stream).Previous: Structure of a program | Index | Next: Constants |