C Data Type Operator expression

Source: Internet
Author: User

String constant
String constants are character sequences enclosed by a pair of double quotes. For example, "China", "C program:", "$12.5" are all valid string constants. String constants and character constants are different quantities. There are mainly the following differences between them:
1. character constants are enclosed by single quotes, and string constants are enclosed by double quotes.
2. A character constant can only be a single character. A String constant can contain one or more characters.
3. assign a character constant to a character variable, but not a String constant to a character variable. There is no corresponding string variable in the C language.
This is different from the basic language. However, a character array can be used to store a String constant. In the array chapter, we will introduce it.
4. character constants occupy the memory space of one byte. The memory bytes occupied by string constants are equal to the number of bytes in the string plus 1. The added byte contains the character "// 0" (ASCII code: 0 ). This is the end sign of the string. For example, the bytes occupied by the string "C program" in the memory are: C program // 0. The character constant 'A' and the String constant "A" both have only one character, but they are different in memory.
'A' occupies one byte in memory, which can be expressed as:
"A" occupies two bytes in the memory, which can be expressed as: A // 0 symbol constant forced type conversion
Forced type conversion is implemented through the type conversion operation. The general form is (type specifier) (expression). Its function is to forcibly convert the operation result of an expression to the type represented by the type specifier. For example, (float) A converts a to a real (INT) (x + y) and converts x + y to an integer. When using forced conversion, pay attention to the following issues:
1. both the type specifiers and expressions must be enclosed in parentheses (single variables can be left empty). For example, you can write (INT) (x + y) as (INT) X + Y converts X to int type and then adds it to y.
2. both force conversion and automatic conversion are temporary conversions of the Data Length of the variable for the purpose of this operation, without changing the type defined for the variable during data description.
Main ()
Float F = 5.75;
Printf ("(INT) F = % d, f = % f // n", (INT) F, F );
F <-- 5.75.
Forcibly convert float F to int F float F = 5.75; printf ("(INT) F = % d, f = % f // n", (INT) F, f); this example shows that, although f is forcibly converted to int type, it only plays a role in the operation and is temporary, and the type of F does not change. Therefore, the value of (INT) F is 5 (with decimals deleted), and the value of F is still 5.75.

Basic operators and expressions

Types, priorities, and associativity of Operators
There are many operators and expressions in C language, which are rare in advanced languages. It is precisely the rich operators and expressions that make the C language function perfect. This is also one of the main features of C language.
The operators in C language not only have different priorities, but also have a feature of their combination. In an expression, the order of computing operations involved in the operation must not only comply with the operator priority, but also be restricted by the combination of operators, to determine whether the operation is performed from left to right or from right to left. This combination is not available for operators in other advanced languages, so it also increases the complexity of the C language.

Types of operators C-language operators can be divided into the following types:
1. Arithmetic Operators
It is used for various numerical operations. Including addition (+), subtraction (-), multiplication (*), Division (/), remainder (or modulo operation, %), auto-increment (++) and auto-subtraction.
2. Relational operators
Used for comparison. Including greater than (>), less than (<), equal to (=), greater than or equal to (> =), less than or equal to (<=), and not equal (! =.
3. logical operators
Used for logical operations. Including and (&), or (|), non (!) Three.
4. bitwise operators
The amount involved in the operation, which is calculated in binary bits. Including bitwise AND (&), bitwise OR (|), bitwise non (~) , Bitwise exclusive or (^), left shift (<), right shift (>.
5. Value assignment operator
Used for value assignment, including simple value assignment (=), compound arithmetic value assignment (+ =,-=, * =,/=, % =), and compound bit operation value assignment (& =, |=, ^ =, >>=, <<=) there are 11 types.
6. Conditional Operators
This is a three-object Operator Used for conditional evaluation (? :).
7. Comma Operator
It is used to combine several expressions into one (,).
8. pointer Operators
This operation is used to obtain the content (*) and the address.
9. Number of cell Operators
Used to calculate the number of bytes of the Data Type (sizeof ).
10. Special operators
There are several types of brackets (), subscripts [], and Members (→.

Priority and combination
In C language, the operator priority is divided into 15 levels. Level 1 is the highest, and level 15 is the lowest. In an expression, a higher priority is calculated prior to a lower priority. When the operator priorities on both sides of a computing workload are the same, the operators are processed in the combined direction specified by the operator's conformances. In C language, operators can be combined into two types: left-to-right (from left to right) and right-to-left (from right to left ). For example, the combination of arithmetic operators is from left to right, that is, first left and then right. If there is an expression x-y + Z, y should first be combined with the "-" number to execute the x-y operation, and then execute the + z operation. This combination direction from left to right is called "left combination ". The combination direction from right to left is called "right combination ". The most typical right concatenation operator is the value assignment operator. For example, x = y = Z. Due to the right combination of "=", you should first execute y = Z and then perform the X = (y = z) operation. Many operators in the C language are right-bound. Pay attention to the differences to avoid understanding errors.

Basic arithmetic operators of Arithmetic Operators and arithmetic expressions
1. The addition operator "+" is a binary operator, that is, two quantities are involved in addition. Such as a + B and 4 + 8. Has the right combination.
2. subtraction operator "-" subtraction operator is a binary operator. However, "-" can also be used as a negative value operator. In this case, it is a single-object operation, such as-X and-5, which has a left combination.
3. The multiplication operator "*" is left-bound.
4. The Division operator "/" is left-bound. When the calculation involved is an integer, the result is also an integer, with the decimal number removed. If one of the computations is real, the result is double-precision.
Void main (){
Printf ("// N // n % d, % d // n", 20/7,-20/7 );
Printf ("% F, % f // n", 20.0/7,-20.0/7 );
Binocular computation is left-bound. When the calculation involved is an integer, the result is also an integer, with the decimal number removed. If one of the computations is real, the result is double-precision. Printf ("// N // n % d, % d // n", 20/7,-20/7 );
Printf ("% F, % f // n", 20.0/7,-20.0/7 );


In this example, the result of 20/7 and-20/7 is an integer and all decimals are removed. And 20.0/7 and-20.0/7 participate in the calculation because of the real number, so the result is also real.
5. Perform binary operations with the remainder operator (modulo operator) "%", which is left-bound. The amount involved in the operation must be an integer. The result of the remainder operation is equal to the remainder after the division of two numbers.
Void main (){
Printf ("% d/N", 100% 3 );
Binocular computation, with left associativity. The remainder operator % requires that the amount involved in the operation be an integer. In this example, Divide 100 by 3 and the remainder is 1.

Auto increment 1, auto increment 1 operator
The auto-increment 1 operator is recorded as "++". Its function is to increase the value of a variable by 1. The "--" operator is used to reduce the value of a variable by 1. Auto-increment 1 and auto-increment 1 operators are single-object operations, all of which have the right combination. You can perform the following operations. -- I minus 1 and then participates in other operations.
After I ++ I is involved in the operation, the value of I increases by 1.
After I -- I is involved in the operation, the value of I is reduced by 1.
I ++ and I -- are prone to errors in understanding and using --. Especially when they are in more complex expressions or statements, it is often difficult to find out, so we should analyze them carefully.
Void main (){
Int I = 8;
Printf ("% d // n", ++ I );
Printf ("% d // n", -- I );
Printf ("% d // n", I ++ );
Printf ("% d // n", I --);
Printf ("% d // n",-I ++ );
Printf ("% d // n",-I --);
} I <-- 8
I <-- I + 1
I <-- I-1
I <-- I + 1
I <-- I-1
I <-- I + 1
I <-- I-1 int I = 8;
Printf ("% d // n", ++ I );
Printf ("% d // n", -- I );
Printf ("% d // n", I ++ );
Printf ("% d // n", I --);
Printf ("% d // n",-I ++ );
Printf ("% d // n",-I --);
The initial value of I is 8.
The output is 9 after adding 1 to the first line I;
3rd rows minus 1, so the output is 8;
4th rows of output I is 8 and then add 1 (9 );
5th the row output I is 9 and then minus 1 (8 );
6th rows output-8 and then add 1 (9 );
7th rows output-9 and then minus 1 (8)
Void main (){
Int I = 5, j = 5, p, q;
P = (I ++) + (I ++ );
Q = (++ J) + (++ J );
Printf ("% d, % d", p, q, I, j );
I <-- 5, j <-- 5, P <-- 0, q <-- 0
I + I ---> P, I + 1 --> I, I + 1 --> I, I + 1 --> I
J + 1-> J, J + 1-> J, J + 1-> J, J + J-> q int I = 5, j = 5, P, q;
P = (I ++) + (I ++ );
Q = (++ J) + (++ J );
In this program, P = (I ++) + (I ++) should be understood as three I additions, so the P value is 15. Then I auto-increment 1 three times is equivalent to adding 3 so the last value of I is 8. Q = (++ J) + (++ J) should be understood as Q first incrementing by 1 before calculation, since Q is incremented by 1 three times and the value is 8, the sum of the Three 8 values is 24, and the final value of J is still 8. An arithmetic expression is a combination of constants, variables, functions, and operators. An expression has a value and its type, which is equal to the value and type of the result obtained by the calculation expression. The expressions are evaluated in the order specified by the operator priority and associativity. A single constant, variable, and function can be considered as a special case of an expression.


Symbol constant
In C, you can use an identifier to represent a constant, called a symbolic constant. A symbolic constant must be defined before it can be used. Its general form is:
# Define identifier constant
Where # define is also a preprocessing command (all preprocessing commands? Quot; # ") is called a macro definition command (which will be further described in Chapter 9 Preprocessing Program). Its function is to define this identifier as its constant value. Once defined, the constant value will be used in all the places where the identifier appears in the program. It is customary to use uppercase letters for the identifier of a symbolic constant, and lowercase letters for the variable identifier to indicate differences.
# Define PI 3.14159
Void main ()
Float S, R;
R = 5;
S = pI * r * R;
Printf ("s = % f // n", S );
The macro-defined command defines PI as 3.14159 S, and R as a real number 5-> r pI * r-> S
Display program results float S, R; r = 5; S = pI * r; before the main function, the macro-defined command defines PI as 3.14159, in the program, replace PI with this value. S = pI * r is equivalent to S = 3.14159 * r. It should be noted that the symbolic constant is not a variable and its value cannot be changed throughout the scope. That is to say, in a program, the assignment statement cannot be used to assign a value again.

Variable initial value and type conversion

Variable Initial Value
In programs, you often need to assign initial values to variables to use them. There are multiple methods in a language program. The method assigned with the initial value is called initialization. In the variable description, the general form of assigning an initial value is:
Type specifier variable 1 = value 1, variable 2 = value 2 ,......; For example:
Int a = B = c = 5;
Float x = 3.2, y = 3f, Z = 0.75;
Char encoding = 'k', CH2 = 'P ';
Continuous assignment is not allowed in the description. For example, a = B = c = 5 is invalid.
Void main ()
Int A = 3, B, c = 5;
B = A + C;
Printf ("A = % d, B = % d, c = % d // n", A, B, C );
A <--- 3, B <-- 0, C <--- 5
B <-- A + C
Show program running results

Variable type conversion
The data type of the variable can be converted. There are two conversion methods: automatic conversion and forced conversion.

Automatic conversion
Automatic conversion occurs when a hybrid operation of different data types is performed, and is automatically completed by the compilation system. Automatic conversion follows the following rules:
1. If the types involved in calculation are different, convert them to the same type before performing calculation.
2. The conversion is performed in the direction of increasing data length to ensure that the accuracy is not reduced. For example, in int and long operations, convert the int value to long before performing operations.
3. All floating-point operations are carried out with Double Precision. Even if only the float Single-precision expressions are included, they must be converted to the double type before calculation.
4. Char and short types must be converted to int type before calculation.
5. In the value assignment operation, the Data Types of the Values on both sides of the value assignment number are different. The type of the value on the right is converted to the type of the value on the left. If the length of the data type on the right is long on the left, part of the data will be lost, which will reduce the precision, and the missing part will be rounded to the forward. Figure 2? 1 indicates the rules for automatic type conversion.


Void main ()
Float Pi = 3.14159;
Int S, r = 5;
S = r * PI;
Printf ("s = % d // n", S );
PI <-- 1, 3.14159
S <-- 0, r <-- 5
S <-- r * pi
Show program running results
Float Pi = 3.14159;
Int S, r = 5;
S = r * PI;
In this example, Pi is real, S, and R is integer. When executing the S = r * PI statement, both the R and PI are converted to the double type, and the result is also the double type. However, because s is an integer, the value is still an integer and the fractional part is removed.


Related Article

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.