# C operator

## Introduction to C operators and operator precedence

C provides us with a variety of operators that can be used to perform operations on data.

In particular, we can identify different types of operators:

• Arithmetic Operator
• Comparison operator
• Logical Operators
• Compound assignment operator
• Bitwise operator
• pointerOperators
• Structure operator
• Miscellaneous operators

In this blog post, I will use 2 fictitious variables to detail all these variables`a`with`b`as an example.

I will exclude bitwise operators, structural operators, and pointer operators from this list, because I will write a specific blog post specifically for them.

## Arithmetic Operator

In this macro group, I will separate binary operators and unary operators.

Binary operators use two operands to work:

operator Name example
`=` task `a = b`
`+` Add to `a + b`
`-` Subtraction `a - b`
`*` multiplication `a * b`
`/` distribution `a / b`
`%` Modulus `a % b`

Unary operators can only use one operand:

operator Name example
`+` One dollar plus `+a`
`-` One yuan minus `-a`
`++` Increment `a++`or`++a`
`--` Decrease `a--`or`--a`

The difference between`a++`with`++a`that's it`a++`increase`a`Variables after use.`++a`increase`a`Variable before using it.

E.g:

``````int a = 2;
int b;
b = a++ /* b is 2, a is 3 */
b = ++a /* b is 4, a is 4 */``````

The same goes for the decrement operator.

## Comparison operator

operator Name example
`==` Equality operator `a == b`
`!=` Not equal to operator `a != b`
`>` more than the `a > b`
`<` less than `a < b`
`>=` greater than or equal to `a >= b`
`<=` less than or equal to `a <= b`

## Logical Operators

• `!`No (example:`!a`)
• `&&`AND (for example:`a && b`)
• `||`Or (for example:`a || b`)

These operators are very useful when using boolean values.

## Compound assignment operator

These operators are very useful for performing assignments and performing arithmetic operations at the same time:

operator Name example
`+=` Additive distribution `a += b`
`-=` Subtractive distribution `a -= b`
`*=` Multiplicative distribution `a *= b`
`/=` Department allocation `a /= b`
`%=` Mode distribution `a %= b`

## Miscellaneous operators

### Ternary operator

The ternary operator isCYou can use 3 operands, which is a short way to express conditions.

It looks like this:

``<condition> ? <expression> : <expression>``

example:

``a ? b : c``

in case`a`Is evaluated as`true`,then`b`The statement has been executed, otherwise`c`Yes.

The function of the ternary operator is the same as the if/else condition, except that it has a shorter representation time and can be inlined into the expression.

### size

This`sizeof`The operator returns the size of the operand you pass. You can pass variables and even types.

Example usage:

``````#include <stdio.h>

int main(void) {
int age = 37;
printf("%ld\n", sizeof(age));
printf("%ld", sizeof(int));
}``````

## Operator precedence

For all these operators (and more operators that I haven't covered in this article, including bitwise operators, structural operators, and pointer operators), you must be careful when using them together in a single expression.

Suppose we have this operation:

``````int a = 2;
int b = 4;
int c = b + a * a / b - a;``````

What's the value`c`? Do we perform addition before addition and division?

There is a set of rules that can help us solve this problem.

In order to go from low priority to high priority, we have:

• This`=`Assignment operator
• This`+`with`-` BinaryOperators
• This`*`with`/`Operators
• This`+`with`-`Unary operator

Operators also have association rules, except for unary operators and assignments, the rules are always from left to right.

in:

``int c = b + a * a / b - a;``

We execute first`a * a / b`, Since it is from left to right, we can divide it into`a * a`And result`/ b`:`2 * 2 = 4`,`4 / 4 = 1`.

Then we can add and subtract: 4 +1-2.`c`Yes`3`.

However, in all cases, I want to make sure that you realize that you can use parentheses to make any similar expression easier to read and understand.

Parentheses have a higher priority than anything else.

The above example expression can be rewritten as:

``int c = b + ((a * a) / b) - a;``

And we donâ€™t have to think too much.