Last time, we talked about the output part. Today, let’s talk about variables.
Variables are a must-have in all programming languages and they also play a great role in learning.
What are variables?
Variables, as the name suggests, are the quantity of change. Actually, it was mentioned earlier.cout<<a<<b<<c<<d
middle,\(a,b,c,d\)It's a variable.
How to define variables?
The definition method of variables is as follows:
Variable type Variable name = Variable value
Variable Type
type | scope |
---|---|
int (Integer) |
\(-2^{31}\sim2^{31}-1\) |
unsigned int (Unsigned integer type) |
\(0\sim2^{32}-1\) |
short (Short integer type) |
\(-2^{15}\sim2^{15}-1\) |
unsigned short (Unsigned short integer type) |
\(0\sim2^{16}-1\) |
long (Long integer type) |
\(-2^{31}\sim2^{31}-1\) |
unsigned long (Unsigned long integer type) |
\(0\sim2^{32}-1\) |
long long (Extra-long integer type) |
\(-2^{63}\sim2^{63}-1\) |
unsigned long long (Unsigned super long integer type) |
\(0\sim2^{64}-1\) |
float (Single precision floating point type) |
\(-3.4\times10^{38}\sim3.4\times10^{38}\) |
double (Double precision floating point type) |
Negative value range\(-1.7976931348623157\times10^{308}\sim-4.94065645841246544\times10^{-324}\); Positive value range\(4.94065645841246544\times10^{-324}\sim1.7976931348623157\times10^{308}\) |
long double (Long double precision floating point type) |
\(-1.21^{-4932}\sim1.21^{4932}\) |
char (character type) |
ASCII code range\(0\sim255\) |
bool (Bolean, or logical) |
real(\(1\)) or false (\(0\)) |
The most common type we use isint
、long long
、double
、char
、bool
。
Legal variable name
- Cannot include C++ tags;
- Only include letters, numbers, and underscores
_
; - Do not start with numbers.
Now, we can create some variables!
int a=100;
long long b=10000LL;//LL represents a number of type long long
double c=0.5;
char d='A';//A's ASCII code is 65
bool e=true;
bool f=1;//1 is true
bool g=false;
bool h=0;//0 is false