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 variablesawithbas 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 betweena++with++athat's ita++increaseaVariables after use.++aincreaseaVariable 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 caseaIs evaluated astrue,thenbThe statement has been executed, otherwisecYes.

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

ThissizeofThe 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 valuec? 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 firsta * a / b, Since it is from left to right, we can divide it intoa * aAnd result/ b:2 * 2 = 4,4 / 4 = 1.

Then we can add and subtract: 4 +1-2.cYes3.

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.

Download mine for freeC manual


More clang tutorials: