Examples of C-language storage operators and other examples, program language Operators
C Storage Class
The storage class defines the range (visibility) and lifecycle of variables/functions in the C program. These specifiers are placed before their modified types. The following lists the storage classes available in the C program:
Auto register static extern auto Storage Class
Auto Storage
The auto storage class is the default storage class for all local variables.
{ int mount; auto int month;}
The above instance defines two variables with the same storage class. auto can only be used in functions, that is, auto can only modify local variables.
Register Storage Class
The register storage class is used to define local variables stored in registers rather than in RAM. This means that the maximum size of the variable is equal to the size of the Register (usually a word), and The '&' operator of one dollar cannot be applied to the variable (because it has no memory location ).
{ register int miles;}
Registers are only used for variables that require quick access, such as counters. It should also be noted that the definition of 'register 'does not mean that the variable will be stored in the register, it means that the variable may be stored in the register, depending on hardware and implementation restrictions.
Static Storage Class
The static storage class instructs the compiler to keep the existence of local variables within the life cycle of the program, instead of creating and destroying each time it enters and leaves the scope. Therefore, using static to modify local variables can maintain the value of local variables between function calls.
The static modifier can also be applied to global variables. When static modifies a global variable, it limits the scope of the variable to the file that declares it.
Static is the default storage class for global variables. The following two variables (count and road) have a static storage class.
static int Count;int Road;main(){ printf("%d\n", Count); printf("%d\n", Road); }
Extern Storage Class
The extern storage class is used to provide a reference to a global variable, which is visible to all program files. When you use 'pattern', variable names that cannot be initialized will point to a previously defined storage location.
When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to obtain references to Defined variables or functions. In this way, extern is used to declare a global variable or function in another file.
C Operator
An operator is a symbol that tells the compiler to execute a specific mathematical or logical operation. C language has built-in rich operators and provides the following types of operators:
Arithmetic Operators Relational operators logical operators bit operators assignment operators miscellaneous Operators
Arithmetic Operators
The following table shows all Arithmetic Operators supported by C. Assume that the value of variable A is 10 and that of variable B is 20. Then:
Operator |
Description |
Instance |
+ |
Add two operands |
A + B will get 30 |
- |
Subtract the second operand from the first operand |
A-B will get-10 |
* |
Multiply two operands |
A * B will get 200 |
/ |
Numerator divided by denominator |
B/A will get 2 |
% |
Modulo operator, remainder after Division |
B % A will get 0 |
++ |
Auto-increment operator. The integer is increased by 1. |
A ++ will get 11 |
- |
The auto-subtraction operator reduces the integer by 1. |
A-will get 9 |
Relational operators
The following table shows all Relational operators supported by C. Assume that the value of variable A is 10 and that of variable B is 20. Then:
Operator |
Description |
Instance |
= |
Check whether the values of the two operands are equal. If they are equal, the condition is true. |
(A = B) is not true. |
! = |
Check whether the values of the two operands are equal. If they are not equal, the condition is true. |
(! = B) True. |
> |
Check whether the value of the left operand is greater than the value of the right operand. If yes, the condition is true. |
(A> B) is not true. |
< |
Check whether the value of the left operand is smaller than the value of the right operand. If yes, the condition is true. |
(A <B) is true. |
> = |
Check whether the value of the left operand is greater than or equal to the value of the right operand. If yes, the condition is true. |
(A> = B) is not true. |
<= |
Check whether the value of the left operand is smaller than or equal to the value of the right operand. If yes, the condition is true. |
(A <= B) is true. |
Logical operators
The following table shows all relational logical operators supported by C. Assume that the value of variable A is 1 and that of variable B is 0, then:
Operator |
Description |
Instance |
&& |
It is called logic and operator. If both operands are non-zero, the condition is true. |
(A & B) is false. |
|
|
It is called a logic or operator. If either of the two operands is non-zero, the condition is true. |
! |
It is called a logical non-operator. Used to reverse the logic status of an operand. If the condition is true, the logical non-operator will make it false. |
! (A & B) is true. |
Bitwise operators
Bitwise operators act on bitwise AND perform operations one by one. &, | And ^ are shown as follows:
P |
Q |
P & q |
P | q |
P ^ q |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
Assume that if A = 60 and B = 13, they are now in binary format, as shown below:
A = 0011 1100B = 0000 1101-----------------A&B = 0000 1100A|B = 0011 1101A^B = 0011 0001~A = 1100 0011
The following table shows the bitwise operators supported by C. Assume that the value of variable A is 60 and that of variable B is 13. Then:
Operator |
Description |
Instance |
& |
If both operands exist, the binary AND operator copies one digit to the result. |
(A & B) will get 12, that is, 0000 1100 |
|
If it exists in any operand, the binary OR operator copies one digit to the result. |
( |
^ |
If it exists in one of the operands but does not exist in both operands, the binary XOR operator copies one digit to the result. |
(A ^ B) 49, that is, 0011 0001 |
~ |
The binary complement operator is a unary operator with a "flip" bit effect, that is, 0 is changed to 0. |
(~ A) The value of-61 is 1100 0011, which is A signed binary complement. |
< |
Binary left shift operator. The value of the left operand moves to the number of digits specified by the right operand. |
A <2 will get 240, that is, 1111 0000 |
> |
Binary right shift operator. The value of the left operand moves the number of digits specified by the right operand to the right. |
A> 2 get 15, that is, 0000 1111 |
Value assignment operator
The following table lists the value assignment operators supported by C:
Operator |
Description |
Instance |
= |
A simple value assignment operator assigns the value of the right operand to the left operand. |
C = A + B will assign the value of A + B to C |
+ = |
Add and assign an operator to assign the result of the right operand plus the left operand to the left operand |
C + = A is equivalent to C = C + |
-= |
Subtract and assign the value operator. Assign the result of the left operand minus the right operand to the left operand. |
C-= A is equivalent to C = C- |
* = |
Multiply the right operand by the result of the left operand and assign the value to the left operand. |
C * = A is equivalent to C = C * |
/= |
Divide the left operand by the right operand and assign the value to the left operand. |
C/= A is equivalent to C = C/ |
% = |
Evaluate the modulo and assign the value operator. Evaluate the modulo value of the two operands to the left operand. |
C % = A is equivalent to C = C % |
<= |
Left shift and value assignment operator |
C <= 2 is equivalent to C = C <2 |
>>= |
Right Shift and value assignment operator |
C> = 2 is equivalent to C = C> 2 |
& = |
Bitwise AND value assignment operator |
C & = 2 is equivalent to C = C & 2 |
^ = |
Bitwise XOR and value assignment operator |
C ^ = 2 is equivalent to C = C ^ 2 |
| = |
Bitwise OR and value assignment operator |
C | = 2 is equivalent to C = C | 2 |
Miscellaneous operators? Sizeof & ternary operators (three-object operators)
The following table lists other important operators supported by C, including sizeof and? :.
Operator |
Description |
Instance |
Sizeof () |
Returns the variable size. |
Sizeof (a) returns 4, where a is an integer. |
& |
Return the address of the variable. |
& A; the actual address of the variable is given. |
* |
Point to a variable. |
* A; points to a variable. |
? : |
Conditional expressions |
If the condition is true? The value is X. Otherwise, the value is Y. |
? : UsedSubstitutionIf... Else statement.
If... The else statement is
if (Exp1) Exp2;else Exp3;
The ternary operator is:
Exp1 ? Exp2 : Exp3;
Operator priority in C
Operator priority determines the combination of items in the expression. This affects how an expression is computed. Some operators have a higher priority than other operators. For example, multiplication and division operators have a higher priority than addition and subtraction operators.
For example, x = 7 + 3*2. Here, x is assigned to 13 rather than 20. Because the operator * has a higher priority than +, multiplication 3*2 is first calculated, then add 7.
The following table lists operators from high to low by operator priority. Operators with higher priority appear on the table, and operators with lower priority appear under the table. In expressions, operators with higher priority are preferentially calculated.
Category |
Operator |
Associativity |
Suffix |
() []->. ++ -- |
Left to right |
RMB 1 |
+ -! ~ ++--(Type) * & sizeof |
From right to left |
Multiplication and division |
*/% |
Left to right |
Addition and subtraction |
+- |
Left to right |
Shift |
<> |
Left to right |
Link |
<=> = |
Left to right |
Equal |
=! = |
Left to right |
Bit AND |
& |
Left to right |
Bitwise OR XOR |
^ |
Left to right |
Bit OR |
| |
Left to right |
Logic AND |
&& |
Left to right |
Logic OR |
| |
Left to right |
Condition |
? : |
From right to left |
Assignment |
= + =-= * =/=%=>=<<<=<=^= | = |
From right to left |
Comma |
, |
Left to right |