Lecture Notes - Chapter 1 - 数据类型、运算符与表达式
一些笔记和随想
About the computer…
Recommendations: Turing Machine
When programming, you can imagine the computer as:
- A smart brain for operations/computing (i.e. Processor), but with no memory, e.g.
- Calculate the result of
1 + 2
- Read some number from the terminal
- …
- Calculate the result of
- A scratch paper (i.e. Memory) to store your result, e.g.
- Decalre a variable
int x;
, then the computer will allocate a space in the memory to store the value of variablex
. - Calculate
x + y
, the computer will compute the value ofx + y
, store the intermediate result in memory. (actually you used the memory) - Perform
z = x
, the computer will copy the value ofz
to the value ofx
.
- Decalre a variable
Notes about variable: When you decalre int x;
, the x
itself is just an Identifier
just as we talked about in the lecture. Just imagine what you do in mathematics:
the variable $x$ here is just an identifier, the value of x
is what we need actually.
Now you can take out a pen and a paper, and do like this (in sequential):
- For all variables decalred, allocate some space for each of them to store their value, the order does not matter
- For all operations performed, do the same operation on your paper.
For example:
int main()
{
// allocate some space for x, current value = 1
int x = 1; /* Current Paper: [x = 1] */
int y = 2; /* Current Paper: [x = 1] [y = 2]*/
int z = 2; /* Current Paper: [x = 1] [y = 2] [z = 2] */
// Update the value of z to x + y insteade of the original...
z = x + y; /* Current Paper: [x = 1] [y = 2] [y = 3]*/
...
}
Basic Types
In the last section, when we talk about variables, we do not talk about how we store the values. As we know, computer use bits for storaging to represent all kinds of values.
TL; DR
If you do not have much time, or you have already mastered Types, just read this subsection and go ahead.
- use
int
/long
for $i \in \mathbb Z$, diff in range - use
unsigned int
/unsigned long
for $n \in \mathbb N$, diff in range - use
float
/double
for $\alpha \in \mathbb R$, diff in precision & range - use
char
for one character.
Integers
The integers is the easiest case:
Take a list of 8 bits (0/1), the list can have \(2 ^ 8 = 256\) different permuatations. then we can simply use each permutation to represent one integer, which ranges from 0 to 255 (256 numbers in total). Unsigned numbered are all constructed this way.
00000000 => 0
00000001 => 1
00000010 => 2
...
11111111 => 256
The signed case is similar, but:
- the first bit stand for the sign of number,
- and complement is used for represent negative integers as we talked about in the lecture.
In C, we declare one variable using:
int
– 32 bits (4 bytes) in memory.long
– 64bits (8 bytes) in memory.unsigned
– for unsigned int / long numbers.
Floatings.
IEEE.
Chars
Reference: ASCII
To understand chars, you can just imagine you are refer to a dict: In computer, we only have numbers, and we use integer to represent char. For example
a
is the first character, we use1
to representb
is the second, use2
c
is the third, use3
- …
z
is 26-th, use26
To represent all the characters, we need about 100+ different integers(ASCII Table).
Const Variables, Const Expressions
Each of the followings is const variables / const expressions? (now we talk about traditional c++, not the modern one)
const int x;
3.1415926
1 + 2
"some text"
- let
const char x[] = "some text"
:x
is …?
C++ is really Massy…
Operators
There are multiple types of operators:
- assignment
- increment/decrement
- arithmetic
- logical
- comparison
- member access:
.
- other:
...
,
a ? b : c
Operator Precedence
See: Operator Precedence
I can never remember the precedence, because ()
is useful when coding!
Diff a++
and ++a
let a = 0
++a
: the result of the expression is1
a++
: the result of the expression is0
Diff l-value and r-value
(a + b)++
cause error.
- l-value: has physical address
- r-value: not a l-value.
Address will be covered in future (Pointer & Physical Memory Arrangement)
The a++ + ++a
Never write this code, and never consider this code.
F***!
- For those put similar code in assignments, 0 grade.
- If you are not sure about the evaluation order, just put
()
in your code.
()
is an elegant and easy way.
The ,
expression
You should know about it (because bugs may arise at this point), but never use this expression in your code.
w = ++x || ++y && ++z
expression
Reference: Lazy Evaluation
because the value of ++x
is 2 (stand for true
), the rest of the ||
expression will not be evaluated!
Never use this expression, because different platforms may have different implementation.
Type Conversion
Just test the feature on your machine, and you will get the answer.
About char, refer to the Basic Type - Char
section.