C++ uses a number of keywords to identify operations and data descriptions; therefore, identifiers created by a programmer cannot match these keywords. The standard reserved keywords that cannot be used for programmer created identifiers are:
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_eqHere is the complete list of fundamental types in C++:
| 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) |
| 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 309 551 616 | = 264 (~18 billion billion) |
Declaration of variables
C++ is a strongly-typed language, and requires every variable to be declared with its type before its first use. This informs the compiler the size to reserve in memory for the variable and how to interpret its value. The syntax to declare a new variable in C++ is straightforward: we simply write the type followed by the variable name (i.e., its identifier). For example:
|
|
These are two valid declarations of variables. The first one declares a variable of type
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.If declaring more than one variable of the same type, they can all be declared in a single statement by separating their identifiers with commas. For example:
|
|
This declares three variables (
a, b and c), all of them of type int, and has exactly the same meaning as:
|
|
To see what variable declarations look like in action within a program, let's have a look at the entire C++ code of the example about your mental memory proposed at the beginning of this chapter:
|
|
4
|
Don't be worried if something else than the variable declarations themselves look a bit strange to you.
"News powered by"

No comments:
Post a Comment