C ++ starts from scratch (2) -- what is an expression?

Source: Internet
Author: User
Tags integer numbers

C ++ starts from scratch (2)

-- What is an expression?

This is the beginning of this series. When learning English, the first thing to learn is letters, which is the basis of English. Similarly, in C ++, all code is composed of identifier, expression, statement, and necessary symbols (such as braces, this section describes what an identifier is.

Identifier

An identifier is a letter sequence consisting of uppercase/lowercase English letters, underscores, and numbers. It is used for identification. The identifier is to mark and identify, that is, the name. It can be used as the name of the variable, function, or class mentioned later, that is, it is used to identify the elements in a specific variable, function, or Class C ++.
For example, ABC is a legal identifier, that is, ABC can be used as the name of a variable, function, and other elements, but does not mean ABC is the name of a variable or function, the so-called legal means that any identifier must not start with a number. It can only contain uppercase/lowercase English letters, underscores, and numbers, and cannot contain other symbols, such ,! ^, And cannot be the same as the C ++ keyword. That is, when we give a variable or function a name, we must regard the name as an identifier, and then must meet the above requirements. For example, 12ab_c is not a legal identifier, so we cannot give a variable or function a name such as 12ab_c; AB _12c is a legal identifier, so it can be used as a variable or function name.
The keyword mentioned above. In subsequent statements and descriptions of some declaration modifiers, we will find that C ++ provides some special identifiers as the statement names to identify a specific statement, such as if and while; or provide modifiers to modify elements such as variables and functions to realize semantics or provide specific information to the compiler and connector for optimization and error detection, such as extern and static. Therefore, when naming variables, functions, or other elements, the C ++ keywords such as if and extern cannot be used as names, otherwise, the compiler cannot determine whether a variable (or function or other C ++ elements) or a statement, and thus cannot compile.
If you want an identifier to be the name of a specific variable, function, or class, you need to use a declaration.

Number

C ++, as a computer programming language, processes numbers. Therefore, the basics in C ++ are numbers. C ++ provides two types of numbers: integer and floating-point numbers, that is, integers and decimals. However, because the computer is not actually digitalized (for details, see the type section in C ++ from scratch (III ), therefore, the number of integer types is divided into signed and unsigned integer types, while the floating point numbers are divided into single-precision and double-precision floating point numbers based on the difference in precision, the same integer number also grows the integer and short integer according to the length.
To represent a number in the C ++ code, you can directly write a number, such as 123, 34.23, and-34.34. Because the computer is not based on numbers and leads to the classification of the preceding numbers, C ++ provides a series of suffixes for representation in the code, as shown below:

 UOrU  It indicates whether the number is an unsigned integer, such as 123u.
 LOrL  Indicates that the number is a long integer, such as 123l, while 123ul is the unsigned long integer, while 34.4l is a long double-precision floating point, which is equivalent to a double-precision floating point.
 I64OrI64  Indicates that the number is a long integer, which is defined by the 64-bit operating system. The length is longer than the long integer. For example, 43i64
 FOrF  Indicates that the number is a single-precision floating point number, such as 12.3f.
 EOrE  Indicates the power of a number. For example, 34.4e-2 is 0.344. 0.2544e3f indicates a single-precision floating point number with a value of 254.4.

When no suffix is written, the specific type is determined based on the number of decimal places and digits. For example, 123 indicates the number of signed integers, and 12341434 indicates the number of signed long integers; 34.43 represents a double-precision floating point number.
Why do we need to do so many things, such as signed and unsigned? This is because the computer is not digit-based, but State-based. Details will be detailed in the next article.
As scientific computing, non-decimal numbers, such as hexadecimal and octal are often used. c ++ also provides some prefixes for this purpose.
Add 0x or 0x before the number to indicate that the number is in hexadecimal format, such as 0xf3fa and 0x11cf. Adding 0 in front indicates that the number is represented in octal format. For example, if the value is 0347, the decimal number is 231. However, neither hexadecimal nor octal can be used to represent floating-point numbers. It can only represent integer numbers, that is, 0x34. 343 is incorrect.

String

In addition to providing the basic expression of numbers, C ++ also provides characters and strings. This is just for the convenience of programming, C ++ as a computer language, there is no need to provide strings. However, the basic requirement of a person on a computer is to display the results, and characters and strings are used to display the results because they are easy-to-read characters, therefore, C ++ provides special support for strings.
As mentioned above, a computer only knows numbers, and a character is a text symbol. It is a graphical symbol. To enable the computer to process symbols, you must convert the symbols into numbers in some way. In the computer, this is achieved by creating a ing between the symbols and numbers, that is, a table. A table has two columns: one column is the graphical symbol we want to display, and the other column is a number. With this table, you can create a ing between the graphical symbol and the number. A standard table, called the ASCII code table, has been defined. Almost all computer hardware supports this conversion table to convert numbers into symbols and then display the computation results.
With the above table, if you want to describe the result as "a", check the ASCII code table and obtain that the number corresponding to the "A" graphic symbol is 65, then, the computer will be told to output 65 characters, and "A" will be displayed on the screen ".
This is obviously complicated and abnormal, So C ++ provides characters and strings. When we want to get the serial number of an ascii code table of a certain graphic symbol, we only need to enclose the character through single quotes, for example, 'A'. The effect is the same as that of 65. When more than one character is used, double quotation marks are used to enclose multiple characters, that is, the so-called string, such as "ABC ". Therefore, strings are connected by multiple characters. However, according to the preceding instructions, it is easy to find that a string also needs to be mapped to a number, but its ing is not as simple as a character and can be done through the lookup table. For this, we will introduce the array in subsequent articles and then explain it again.

Operator

The computer is basically a number, so all operations on the computer are to change the number, so normally C ++ provides some basic operations for operating the number, called operators, such: +. Any operator must return a number called the return value of the operator. Therefore, the operator is the operator that operates the number and returns the symbol of the number. As a general classification, operators are divided into one, two, and three operators based on the number of numbers simultaneously used by operators.
Unary operators include:

+ It is followed by a number, and the subsequent number is returned intact.For example, the return value of + 4.4f is 4.4; the return value of +-9.3f is-9.3. It is entirely out of semantic needs. For example, this number indicates a positive number.
- Followed by a number to reverse the symbol of the followed number.For example, the return value of-34.4f is-34.4, And the return value of-(-54) is 54. Indicates a negative number.
! It is followed by a number, and the logic is reversed by a number.Logical values are "true" or "false". To use numbers to represent logical values, it is specified in C ++ that a non-zero value is logical truth, while a zero value is logical false. Therefore, 3, 43.4, and 'A' indicate logical truth, while 0 indicates logical false. The logical value is applied to subsequent judgment and loop statements. The opposite of logic is to judge "!" first. The number followed by logical truth or logical false is then reversed. For example:
! The return value of 5 is 0, because 5 is not zero but the logic is true, and then the inverse logic is false. Therefore, 0 is returned.
!! The return value of 345.4 is 1. First, because 345.4 is non-zero, the logic is true. Then, the reverse logic is false, and the reverse logic is true. Although non-zero logic is true, 1 is used to represent true logic as the logic returned by the compiler.
~ Followed by a number.An operation defined in logic cannot be used as a number. In order to apply the inverse operation to the number, the number is represented in binary, then, the bitwise operation is performed on each digit of the number (because each digit of the binary number can only be 1 or 0, which is exactly in line with the true and false logic ). For example ~ The return value of 123 is-124. First, convert 123 to the binary number 01111011. Then you get the inverse 10000100 and the last-124.
The problem here is why it is 8 bits instead of 16 bits. Because 123 is less than 128, it is positioned as char type, so it is 8 bits (about what Char is next ). If yes ~ The return value is 4294967172.
Why is there a number to reverse this operation? Because the CPU provides such commands. It also has a very good and important application, which will be introduced later.

Other unary operators will be mentioned later (but not all ).
Binary operators include:

+
-
*
/
%
Returns the sum, difference, product, quotient, and remainder of the two numbers.For example:
The return value of 34 + 4.4f is 38.4; the return value of 3 +-9.3f is-6.3.
The return value of 34-4 is 30; the return value of 5-234 is-229.
3*2 returns 6; 10/3 returns 3.
10% 3 returns 1; 20% 7 returns 6.
&&
|
Each of the preceding and following logic values returns the "and" operation logic value and "or" operation logic value.For example:
The return value of 'A' & 34.3f is logical truth. the return value of 1; 34 & 0 is logical false, and the return value is 0.
0 | the return value of 'B' is logical truth, which is 1; 0 | the return value of 0 is logical false, Which is 0.
&
|
^
Returns the "and" operation, "or" operation, and "XOR" Operation values of the two numbers.As mentioned above, convert the numbers on both sides into binary numbers, and then perform operations such as, or, or. For example:
The return value of 4 & 6 is 00000110 to 00000100, and the value of 6 is, which is 4.
4 | the return value of 6 is 6, 4 is converted to 00000110, and 6 is converted to 00000110, which is 6.
The return value of 4 ^ 6 is 00000110 to 00000010, and the value of 6 is, which is 2.
>
<
=
> =
<=
! =
Each digit is followed by a number. The corresponding logical value is returned based on whether the two digits are greater than, less than, equal to, greater than or equal to, less than or equal to, and not equal.For example:
The return value of 34> 34 is 0, which is logical false. The return value of 32 <345 is 1, which is logical truth.
The return values of 23> = 23 and 23> = 14 are both 1, which is logical truth. the return value of 54 <= 4 is 0, which is logical false.
The return value of 56 = 6 is 0, which is logical false; the return value of 45 = 45 is 1, which is logical truth.
5! = The return value of 5 is 0, which is logical false; 5! The return value of = 35 is true, which is logical.
>
<
Each digit is followed by a number, and the number on the left is shifted to the right or the number on the right is shifted to the specified number of digits on the right.With the previous ~ , &, | And other operations are the same. The reason why left-and right-shift operations are required is that the CPU provides these commands and is mainly used to compile some binary algorithms.
<Convert the number on the left to a binary number, and then move the number of digits from the left to the right, for example, 4, to 00000100. If you move the value two places to the left, the value is 00010000, and the value is 16.
> Like <, it only moves to the right. For example, if the value is 6, it is converted to 00000110. If the value is 1 to the right, it is changed to 00000011. 3 is required. If two digits are removed, one digit exceeds the limit and the value is truncated. The value 6> 2 returns 00000001, which is 1.
What is the use of left shifting and right shifting? It is used in some binary algorithms, but it can also be used as a simple optimization method. Considering the decimal number of 3524, we shifted it to 2 places to 352400, which is 100 times larger than the original number. To be accurate, it should be twice the power of 10. If 3524 is shifted to two places to 35, it is equivalent to dividing the original number by the operator of 100.
Similarly, the first 4> 2 is equivalent to the quotient of 4/4; 32> 3 is equivalent to 32/8, that is, the quotient of 32 divided by 2 to the power of 3. 4 <2 is equivalent to 4*4, which is equivalent to 4 multiplied by 2 to the power of 2. Therefore, the left and right shifts are equivalent to multiplication and division, but they can only be multiplication or division by the power of the corresponding hexadecimal number, but their operation speed is much higher than multiplication and division, therefore, it is a simple optimization method.
, Each digit is followed by a number, and the right digit is simply returned.For example:
34.45f, 54 returns 54;-324,454 5f returns 4545f.
So what is the use of it? It is used to combine multiple numbers into a single number, which will be further described in C ++ from scratch (4.

Other binary operators will be mentioned later (but not all ).
There is only one ternary operator, which is? :. The format is: <number 1>? <Number 2>: <number 3>. The return value is: If <number 1> is logical, <Number 2> is returned; otherwise, <number 3> is returned. For example:
34? The return value of 4: 2 is 4. Because 34 is non-zero and logical, 4 is returned. And 0? The return value of 4: 2 is 2. Because 0 is logical false, 2 is returned.

Expression

You should find out what is ridiculous. -- 12> 435 returned value is 0. Why don't you write 0 directly? This is the meaning of the expression.
">" Is followed by a number, but the operator operates a number and returns the symbol of the number because it returns a number, therefore, it can be placed in any of the above-mentioned places that require numbers to be connected, which forms a so-called expression. For example, the return value of 23*54/45> 34 is 0, because the return value of 23*54 is 1242, and then 1242 is used as the left-side number of "/" to get a new return value of 27.6; finally, use 27.6 as the left-side number of ">" to obtain the return value 0, which is logical false.
Therefore, an expression is a piece of code composed of a series of items that return numbers and operators. It is composed of operators, so it must return values. The "Return numeric" mentioned above can be another expression, a variable, a function with a returned value, or an object of a class with a numeric operator overload, in any case, a number is returned. If you are unfamiliar with variables, functions, classes, and other terms, you do not need to worry about them. This will be explained in subsequent articles.
Therefore, 34 is also an expression, and its return value is 34, but it is an expression without an operator (we will know later that 34 is actually an operator ). Therefore, the concept of expressions is actually very broad. Anything that has returned values can be called expressions.
Because there are many operators in the expression, the execution operator order depends on the operator priority. Just like in mathematics, *,/has a higher priority than + ,-, and logical operators such as +,-, and greater than> and <. You do not need to deliberately remember the operator priority. When the operator execution sequence cannot be determined, you can use parentheses to specify the operator's priority. For example:
(1 + 2) * 3) + 3)/4 returns 3, while 1 + 2*3 + 3/4 returns 7. Note that 3/4 is 0, because 3/4 of the vendors are 0. To divide or multiply a floating point number, you only need to set one of the operands as a floating point number. For example, 3/4. 0 returns 0.75.

& | ^ ~ Applications

We mentioned the logical operators "&", "|", and "!". Is provided by C ++. But why is there an operator that converts a number to a binary number and then performs logical operations on the binary number? First, the CPU provides the corresponding commands, and it also has the following very meaningful applications.
Consider a 10-character intersection. Each intersection has three traffic lights, indicating whether to turn left, turn right, and go straight. There are a total of 12. Now I want to write a control program for it. No matter what the function of this program is, first I need to convert the status of the traffic lights into numbers, because the computer only knows the numbers. Therefore, three numbers are used to represent the three traffic lights at an intersection. Therefore, the status of each traffic light is represented by a number. If the red light is 0, the green light is 1 (do not consider the yellow light or other circumstances ).
Later, I suddenly found that, in fact, I could also use a number to represent the status of three traffic lights at an intersection. For example, I would use 110 to represent a left-turn green light, a straight green light, and a right-turn red light. The above 110 is a decimal number, and every bit of it can actually be 0 ~ It is a waste of nine digits, but only two digits: 0 and 1 are used here. Therefore, if the binary number is used, it is still 110, but it is a binary number. If it is converted to 6, it is only 7 even if it is 111, it is much smaller than the decimal number above 110, saving ......?? What ??
The number 235425234 on paper must occupy more paper than the number 134 (assuming the word size is the same ). Therefore, it takes more resources to record a large number than to record a small number. This is ridiculous! Whether it is 100 or 1000, it is only a number. Why is it more resource-consuming to record large numbers? Because a computer is not a digital computer, but an electronic computer, it is based on the status rather than the number, which will be detailed in the next article. A computer must use some representation to represent a number. The representation is similar to binary, but not binary. Therefore, a large number of smaller records consumes more resources, that's why the number of integers in the preceding section is divided into long integers and short integers.
Let's continue with the above thinking. The binary number 110 is used to indicate the status of the three traffic lights. Now you need to know what status the number 110 represents. The third digit indicates the left turn, but the computer does not know this, so it is as follows: 110 & 100. The return value of this expression is 100, non-zero, and logical. Assume that the status of an intersection is 010, then the same 010 & 100, the return value is 0, and the logic is false. Therefore, the "&" operator can be used to extract the status of one or several digits in the binary number. So when we want to know whether a number indicates a left-turn traffic light in the traffic light status is a green light, we only need to make it and the 100 phase.
Now we need to keep the status of other traffic lights unchanged. We only need to set the left turn traffic light to the green light. For example, the current status is 010. To set the left turn traffic light to the green light, the value should be 110, this can be done through 010 | 100. If the current status is 001, 001 | 100 is 101. correct: the traffic light status of the direct and right directions does not change. Therefore, you can use the "|" operator to set the status of one or more digits of a binary number, but only 1. If you want to set it to 0, such as 101, to turn off the green light of the left turn, 101 &~ 100, the return value is 001.
The preceding mentioned intersection traffic light status can be represented by a variable in actual compiling, and the above 100 can also be represented by an identifier, such as State & ts_left, it can be used to check the status of the Left turn traffic light in the State indicated by the variable state.
The above method is widely used. For example, if you create a window and a window may have 20 or 30 styles, you can use the above method, you can use only a 32-bit binary number to represent the window style, instead of having to get 20 or 30 numbers to indicate whether each style has either.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.