Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the C language operator

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use C language operator". In daily operation, I believe many people have doubts about how to use C language operator. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use the C language operator! Next, please follow the editor to study!

Operators and expressions

Operator: the operator is used to perform program code operations against more than one Operand. For example: 2x 3, whose operands are 2 and 3, and the operator is "+"

Operand: the entity on which the operator acts, and the Operand indicates the source of the data required for the operation performed by the instruction.

Expression: a combination of numbers, operators, numeric grouping symbols (parentheses), free variables, etc., in order to obtain a meaningful arrangement of values.

Expression writing rules

1, operators cannot be adjacent. For example, axiom Musb is wrong.

2, the multiplication sign cannot be omitted. Column x multiplied by y, should be written as xroomy, not xy

3. Multi-layer parentheses can be used to indicate the order of operation. Parentheses must appear in pairs, all using parentheses.

Operator precedence and associativity

The so-called priority is that when there are multiple operators in an expression, who is evaluated first and who is evaluated later. In fact, we learned this when we were learning arithmetic in primary school, such as 1-4 / 2.

So what is "combination"? Priority is all about who is calculated first and then who is calculated when operators with different priorities participate in the operation. But if the operator has the same precedence, who is evaluated first and then who? This is determined by "combination".

For example, 1 times 2 × 3 / 4, the priority of multiplication and division is the same, but the calculation is from left to right, that is, multiplication and division are calculated first and then division, so the combination of multiplication and division is from left to right. It's that simple!

Operator classification

Classified by required operands:

Unary operator

Binocular operator

Ternary operator

Classified by function:

Arithmetic operator

Monocular positive or negative does not change the value of the Operand. Int num = 10

Is the value of the-num;// expression-10 num or 10

Division of integers rounds down. 5ax 2 = = 2

5Accord 2.0 = = 2.5

The modular operation is only for integers. 5% 2 = 1

5.2% 2move / syntax error

Assignment operator

Assign a data (constant or expression) to a variable. Int age = 18

Age + = 2 Bandram age = age + 2

Age+ = 5-3; age+ (5-3)

Relational operator

There are only two values for relational expressions, returning 1 for true and 0 for false.

The so-called relational operation is to make comparisons. In daily life, we often encounter some true and false judgments, such as "Zhang San is higher than Li Si Gao", "Jinan to Beijing is closer than Jinan to Shanghai", and "5 is greater than 2". The answer is true or false. Programming is a simulation of the actual problem-solving process, and it is often necessary to make judgments, such as "if this, I will execute action A, if that, I will execute action B", how to judge this or that? relational operators and relational expressions are required.

Logical operator

Logical expressions have only two values, returning 1 for true and 0 for false.

It is generally used to join relational expressions.

Short circuit phenomenon

In logical expressions composed of & & and | | operators, C language has a principle of "saving calculation". When you can determine the value of the entire logical expression after calculating the value of a subexpression, the subsequent subexpression does not need to be evaluated, which is also called a short-circuit expression.

For example: false & & printf ("1\ n"); true & & printf ("2\ n"); false | | printf ("3\ n"); true | | printf ("4\ n"); / / output: 2 / / 3

For the & & symbol, if the left expression is false, the right expression will not be executed, that is, printf ("1") will not be executed; if the left expression is true, the right expression will be executed, that is, printf ("2") will be executed and output 2

For the | | symbol, if the left side is true, the right expression will not be executed, that is, printf ("4") will not be executed. If the left expression is false, the right expression will be executed, that is, printf ("3") will be executed and output 3.

Special operator

Special arithmetic operator

Perform + 1 and-1 operations on variables.

Can only be used with variables, not with constants and expressions

The prefix is calculated first and then used.

When setting the post, it is used first and then operated.

Note: do not use nesting

Int a = 1; printf ("% d", (a + + + a)); / / 1 + 3 comma operator

Concatenate multiple expressions with commas, also known as the sequential evaluation operator. The value of the entire expression is the value of the expression after the last comma.

Comma expressions serve two purposes:

Segmentation: int a _ r _ b _ c _ r _ d

Evaluation: int a = (1, 1, 3, 5, 7, 9)

Int b = 1, 3, 5, 7, 9

Conditional expression

Conditional expressions can judge the specified conditions, and true and false execute different code respectively.

Terms? Expression 1: expression 2

Printf (5 > 0? "positive": "negative"); printf ((5 > 0)? ("positive"): ("negative")); / / to avoid dazzling, you can add parentheses bit operators to each expression

Bit operations are only for integers and characters, and operate directly on binary

Bit by bit and &

In fact, this symbol has the same meaning as logic and operation & & except that it acts on everyone.

If there is 0, then 0: for each person, both numbers are true, then they are true, otherwise they are false.

3-> 0b 0000 0011 2-> 0b 0000 0010 3 cycles 2-> 0b 0000 0010 bitwise or |

In the same way, and logic or operation | | the meaning is the same, but it works on everyone.

There is one rule 1: for everyone, whatever is true is true, otherwise it is false.

3-> 0b 0000 0011 2-> 0b 0000 0010 3 | 2-> 0b 0000 0011 bitwise XOR ^

The same is 0, the difference is 1: for everyone, as long as the same is 0, the difference is 1.

3-> 0b 0000 0011 2-> 0b 0000 0010 3 ^ 2-> 0b 0000 0001 bitwise inversion

Inversion is a monocular operator that inverts each bit, that is, 0 to 1, 1 to 0.

6-> 0b 0000 0110 ~ 6-> 0b 1111 1001 / / complement: the highest bit becomes a symbol bit, so the result must be a negative number 0b 1000 0110 / / reverse code 0b 1000 0111 / / the original code result:-7 move to the left

The operation of moving to the left is to move the whole of these 0 ah 1 ah to the left by n bits, and add 0 to what is missing on the right.

3-> 0b 0000 0011 30b 0000 0010 2 > 1-> 0b 0000 0001 = = 1 2-> 0b 0000 0010 2

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How JavaScript displays all links

    This article is about how JavaScript displays all the links. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look. Show all links add the following code to the area

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report