C operators

C

C language operators can be unary, binary or ternary, each having a clear precedence and associativity. The following table summarizes the operators of the C language. Operators are presented in descending order of priority.

Precedence Operator Description Associativity
1 Index left-right
1 . and -> left-right member selection
1 ++ and – Postincrementare / postdecrementare left-right
2 ! Right-to-left logical negation
2 ~ Complement vs. 1 per right-left bit
2 ++ and – Preincrementation / Right-left predecrement
2 + and – + and – right-left joints
2 * Right-left derefentiation
2 & operator address right-left
2 (type) Right-to-left conversion
2 sizeof () Size of right-left bytes
3 * left-right multiplication
3 \ Left-right division
3% The rest of the left-right split
4 + and – Collect \ left and right decreases
5 << and >> Left / Right left / right bit shift
6 <Less left-right
6 <= Less than or equal to left-right
6> Larger left-right
6> = Higher or equal left-right
7 == Equals left-right
7! = Different left-right
8 & SI on left-right bits
9 OR OR EXCLUSIVE on left-right bits
10 | OR on the left-right bits
11 && AND logic left-right
12 || OR logical left-right
13?: Right-to-left conditional operator
14 = Right-left assignment
14 + = and – = Attribution with right-left addition and addition
14 * = and / = Attribution by right / left multiplication / division
14% = Attribution with modulo
14 & = and | = Attribution with AND / OR right-left
14 ^ = Attribution with OR-EXCLUSIVE straight-left
14 << = and >> = Attribution with right-left bit shift
15. left-right sequence operator

Consideration should be given to the precedence of operators to obtain the expected results. If some types of precedence (such as arithmetic operators) are obvious and do not (apparently) have problems (and because of their frequent use), others can lead to hard to find errors. For example, the following code snippet does not produce the desired result because:
f (flags & MASK == 0) {
  …
}

first is the equality that produces as a result (0 for False, and 1 for True) followed by the bit between the flags and 1. To get the desired result, the brackets will be used:
if ((flags & MASK) == 0) {
  …
}

now first will be AND on the bits between the flags and the MASK, after which equality is checked. An expression is a sequence of operands and operators (valid from the point of view of C language syntax) that performs one of the functions: calculating a value, designating an object (variable) or functions, or generating a side effect. Another common mistake is the misuse of operators = and ==. The first is attribution, the second equality comparison. Errors often occur as:
if (a = 2) {
  …
}

The compiler considers the correct condition because it is a valid expression in the assigning C language, which is always valued at a nonzero value. Sometimes it is useful to measure the execution time of a particular part of a program or even the entire program. To do this, we can use the clock () function of the time.h. Header file. This function returns an approximation of the number of clock cycles past the start of the program. To get a value in seconds, we divide that value to the constant CLOCKS_PER_SEC.

Recent Posts

Archives

Categories