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 variablesa
withb
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 betweena++
with++a
that's ita++
increasea
Variables after use.++a
increasea
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 casea
Is evaluated astrue
,thenb
The statement has been executed, otherwisec
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
Thissizeof
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 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 * a
And result/ b
:2 * 2 = 4
,4 / 4 = 1
.
Then we can add and subtract: 4 +1-2.c
Yes3
.
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:
- Introduction to C programming language
- C variables and types
- C constant
- C operator
- C conditional
- How to use loops in C
- Introduction to C arrays
- How to determine the length of an array in C
- Introduction to C strings
- How to find the length of a string in C
- C pointer introduction
- Iterate over the array in C
- Boolean in C
- Introduction to C functions
- How to use NULL in C
- Basic I/O concepts in C language
- Double quotes and single quotes in C
- How to return a string from a C function
- How to solve the implicit declaration library function warning in C language
- How to check character value in C
- How to use `printf()` to print percent characters in C language
- C conversion specifiers and modifiers
- How to access command line parameters in C
- Range of variables in C
- Can functions be nested in C?
- Static variables in C
- C global variables
- Typedef keyword in C language
- C enumeration type
- C structure
- C header file
- C preprocessor