C++ notes — variables and primitive types (part 1)
“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”
1. Basic built-in types
C++ basic data types include arithmetic type and null type
1.1 Arithmetic types
Arithmetic types include characters, integers, Booleans, and floating point numbers
-
Characters: Basic character type char, 8 bits, 1 byte. The extended character set includes wCHAR_t, CHAR16_t, and char32_t.
- Wchar_t: wide character, 16 bits
- Char16_t: Unicode character, 16 bits
- Char32_t: indicates a Unicode character of 32 bits
-
Integer type: short, int, long, and long long
- Short: a short integer with at least 16 bits
- Int: An integer of at least 16 bits, usually 32 bits
- Long: The length is at least 32 bits
- Long long: long integer, at least 64 bits (C++11)
Note: Integers can be divided into signed and unsigned. The range of numbers that can be represented by unsigned integers is twice that of signed integers. The highest bit of signed integers is the sign bit 0 for positive and 1 for negative. Choose unsigned type when you know that the value of an integer variable is non-negative
- Boolean values (bool) : true, false (false)
- Float: float, double, long double
- Float: a single-precision floating point number with at least six significant digits. The total size of float is 32 bits
- Double: a floating-point number with at least 10 significant digits, 16 significant digits, and a total size of 64 bits
- Long double: Extended precision floating point number with at least 10 significant digits. The total size of long double is usually 128 bits
1.2 Type Conversion
The compiler automatically converts one type to another related type
Common type conversions
-
The assignment
#include<iostream> int main(int argc, char const* argv[]) { float x = 1.302; int y = x; / / 1 bool z = y; //true double a = z; / / 1.0 unsigned int b = - 1;// Base 2 corresponds to an unsigned value, 0xFFFFFFFF int c =2e32;// uninitialized, vc2019c is initialized to 0; std::cout <<x<<'\n' << y << '\n' << z << '\n' << a << '\n' << b << '\n' << c << std::endl; return 0; } Copy the code
-
judge
int key=302; if(key){}// The key value is automatically converted to bool. The key does not change Copy the code
Note:
- The loop condition cannot be whether unsigned is less than 0, because unsigned numbers are never less than zero
1.3 Literal constants
- Integer, floating point, character, string literal
As the name implies, concrete numbers, characters, strings, booleans. For example: 2,4,6,8,3.14, 0xff, “hello world” and so on
- Escape sequences
A generalization is an escape sequence of \ and other characters, such as ‘\t’,’\n’,”\tHHH”
- Specifies the literal type
// Character/stringPrefix + character/string U ->char16_t ; U -> char16_t ; L -> wchar_t; u8 ->char
// Integer/floating pointInteger/floating point + suffix integer: u/ u ->unsigned; l/L -> long ; ll/LL -> long longFloat type: f/ f ->float; l/L -> long double
Copy the code
- Boolean and pointer literals
Bool: true, false. Pointer: nullptr (C++11)
2, variables,
Variables and objects can be understood to mean the same thing
2.1 Variable Definition
- Initialize the
When an object obtains a specific value at creation time, we say the object is initialized.
Note: Initialization is not assignment. Initialization means creating a variable with an initial value. Assignment means erasing the current value and writing a new value
- List initialization
Such as:
vector<int> x{1.2.3.4};
int c={2};
Copy the code
Note: The compiler will issue an error when listing initialization is at risk of losing information, such as int c={2.5};
- Default initialization
Any variable outside the function body that is not initialized is initialized to zero by default. Any variable inside the function body that is not initialized is not initialized. Accessing or copying this variable will result in an error
2.2 Variable declaration definition
Variable declarations add the extern keyword before the variable name
extern char o;/ / declare
char j;/ / define
Copy the code
Variables can be declared more than once and cannot be defined more than once
2.3 the identifier
The variable name consists of alphanumeric underscores (_) and must start with a letter or underscore (_). It is case-sensitive and cannot conflict with keywords. The variable name can be camel name or underscore name
2.4 Name scope
Generally, it is bounded by curly braces and can be passed to the inner brackets (if the names conflict, the inner brackets prevail), but not to the outer brackets
#include<iostream>
#include<vector>
int cd;
int main(int argc, char const* argv[])
{
unsigned int i = 10,l=30;
int j =- 20;
j = i +j;
{
int j = 30;
std::cout << j << std::endl;
}
std::vector<int> x{ 1.2.3.4 };
std::cout << cd<< std::endl;
return 0;
}
Copy the code
The output is 30, minus 10
By naming the outer scope, you can access the outer variables through the domain name and the scope symbol
#include<vector>
namespace JJJ {
int j = - 20;
}
int cd;
int main(int argc, char const* argv[])
{
unsigned int i = 10,l=30;
int j = - 20;
j = i +j;
{
int j = 30;
std::cout <<JJJ::j << std::endl;
}
std::vector<int> x{ 1.2.3.4 };
std::cout << j<< std::endl;
return 0;
}
Copy the code