C Language Operators: Complete Guide
There are many types of operators in the C language, such as arithmetic operators, relational operators, logical operators, bitwise operators, etc. It is important to pay attention to the priority and associativity of operators.
Common C language operators include:
- Arithmetic operators: addition, subtraction, multiplication, division, remainder
- Relational operators: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)
- Logical operators: && (logical AND), || (logical OR), ! (logical NOT)
- Bitwise operators: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift)
- Assignment operators: =, +=, -=, *=, /=, %=
- Increment and decrement operators: ++, —
- Ternary operator: ?:
- The sizeof operator: returns the size (in bytes) of a variable or data type.
In the C language, operators have precedence and associativity. Operators with higher precedence are calculated first, and parentheses can be used to change the priority of operators when unsure. For operators with the same associativity, calculations are done in either a left-to-right or right-to-left order.
For example, in the expression a + b * c, the multiplication operator takes precedence over the addition operator, so b * c will be evaluated first before being added to a.