- C++ Fundamentals
- Antonio Mallia Francesco Zoffoli
- 1085字
- 2021-06-11 13:35:57
Built-in Data Types
In most programming languages, data is stored in variables, which are labels that refer to the part of memory defined by the programmer. Each variable has an associated type. The type defines what kind of values the variable can hold.
The built-in data types of C++ are divided into two categories:
- Primitive data types: Can be used directly by the user to declare variables
- Abstract or user defined data types: Are defined by the user, for example, to define a class in C++ or a structure
Primitive Data Types
Primitive data types consist of the following types:
- Integer: The int type stores a whole number value ranging from -2147483648 to 2147483647. This data type usually takes up 4 bytes of memory space.
- Character: The char type stores character data. It is guaranteed to be big enough to represent any UTF-8 single byte code unit; for UTF-16 and UTF-32, char16_t and char32_t are used, respectively. char typically takes 1 byte of memory space.
- Boolean: The bool data type is capable of holding one of two values: true or false.
- Floating-point: The float type is used for storing single precision floating point values. This data type usually takes up 4 bytes of memory space.
- Double floating point: The double type is used for storing double precision floating point values. This data type usually takes up 8 bytes of memory space.
- Void: The void type is a valueless data type that is used for functions that do not return a value.
- Wide character: The wchar_t type is also used to represent character sets, but allows for greater size. While char supports characters between 8 and 32 bits, a wide character is 2 to 4 bytes long.
The character types char and wchar_t hold numeric values corresponding to the characters in the machine's character set.
Datatype Modifiers
The numeric types offered by the C++ programming language fall into three categories:
- Signed
- Unsigned
- Floating point
The signed and unsigned types come with different sizes, which means each of them can represent a smaller or larger range of values.
Integer types can be signed or unsigned, where signed types can be used to distinguish between negative or positive numbers, while unsigned can only represent numbers greater than or equal to zero.
The signed keyword is optional; the programmer only needs to specify it if the type is unsigned. Thus, signed int and int are the same types, but they are different from unsigned int, or just unsigned for brevity. Indeed, if it is not specified, an unsigned type always defaults to int.
Integers, as previously mentioned, can come in different sizes:
- int
- short int
- long int
- long long int
The short int type, or just short, is guaranteed to be at least 16 bits according to the standard. This means it can hold values in the range of -32768 to 32767. If it was also unsigned, so unsigned short int or just unsigned int, this range would be 0 to 65535.
Note
The effective size in memory of types can change based on the platform for which the code is compiled. C++ is present in many platforms, from supercomputers in data centers to small embedded chips in industrial settings. To be able to support all these different types of machines, the standard only sets the minimum requirements on built-in types.
Variable Definition
A variable is named storage that refers to a location in memory that can be used to hold a value. C++ is a strongly-typed language and it requires every variable to be declared with its type before its first use.
The type of the variable is used by the compiler to determine the memory that needs to be reserved and the way to interpret its value.
The following syntax is used to declare a new variable:
type variable_name;
Variable names in C++ can contain letters from the alphabet, both upper and lower case, digits and underscores (_). While digits are allowed, they cannot be the first character of a variable name. Multiple variables of the same type can all be declared in the same statement by listing their variable names, separated by commas:
type variable_name1, variable_name2, …;
This is equivalent to the following:
type variable_name1;
type variable_name2;
type ...;
When declaring a variable, its value is left undetermined until an assignment is performed. It is also possible to declare a variable with a given value; this operation is also referred to as variable initialization.
One way – and probably the most common one – to initialize a variable, also referred to as C-like initialization, uses the following syntax:
type variable_name = value;
Another solution is constructor initialization, which we will see in detail in Lesson 3, Classes. Constructor initialization looks like this:
type variable_name (value);
Uniform initialization or list initialization introduces brace initialization, which allows for the initialization of variables and objects of different types:
type variable_name {value};
Demystifying Variable Initialization
When a variable is initialized, the compiler can figure out the type needed to store the value provided, which means that it is not necessary to specify the type of the variable. The compiler is indeed able to deduct the type of the variable, so this feature is also referred to as type deduction. For this reason, the auto keyword has been introduced to replace the type name during initialization. The initialization syntax becomes this:
auto vvariable_name = value;
Another way to avoid directly providing a type is to use the decltype specifier. It is used to deduce a type of a given entity and is written with the following syntax:
type variable_name1;
decltype(variable_name1) variable_name2;
Here, variable_name2 is declared according to the type deducted from variable_name1.
Note
Type deduction using the auto and decltype keywords has been introduced by the C++11 standard to simplify and facilitate variable declaration when the type cannot be obtained. But at the same time, their extended use when not really needed can reduce code readability and robustness. We will see this in more detail in Lesson 4, Generic Programming and Templates.
In the following code, we will check a valid statement for variables by creating a new source file named main.cpp and analyzing the code one line at a time.
Which one of the following is a valid statement?
int foo;
auto foo2;
int bar = 10;
sum = 0;
float price = 5.3 , cost = 10.1;
auto val = 5.6;
auto val = 5.6f;
auto var = val;
int a = 0, b = {1} , c(0);
- Google Flutter Mobile Development Quick Start Guide
- Full-Stack Vue.js 2 and Laravel 5
- Full-Stack React Projects
- Expert Android Programming
- FFmpeg入門詳解:音視頻原理及應(yīng)用
- ADI DSP應(yīng)用技術(shù)集錦
- 計算機應(yīng)用基礎(chǔ)實踐教程
- Learning Concurrent Programming in Scala
- Java EE企業(yè)級應(yīng)用開發(fā)教程(Spring+Spring MVC+MyBatis)
- JBoss:Developer's Guide
- Python自然語言理解:自然語言理解系統(tǒng)開發(fā)與應(yīng)用實戰(zhàn)
- Learning Android Application Testing
- 大學(xué)計算機基礎(chǔ)實訓(xùn)教程
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)
- 計算機信息技術(shù)實踐教程