C language Operators and expressions

The main thing what microcontrollers does is operates with data. There are four main operations that microcontrollers does: adds, abstracts, multiplies and divides (+,-,*,/). division can be split in division “/” and modulus operation “%”. For instance i1/i2 is integer division.

Other part of operators is Relation operators. They are used for boolean conditions and expressions. Expressions with these operators return true or false values. Zero is taken as false and non zero value as true. Operators may be as follows: <, <=, > >=, ==, !=.

The priority of the first four operators is higher than that of the later two operators. These operators are used in relational expressions such as:

7 > 12 // false 20.1 < 20.2 // true 'b' < 'c' // true "abb" < "abc" // true

Note that the equality operator is == and not =. ‘=’ is an assignment operator.

If you want to compare a and b for equality then you should write a == b, not a = b because a = b means you are assigning the value of b to a, as shown in.

Next part of operators are logical operators. With logical operators results may be combined to get some logical results. Logical operators may be: negation “!”, logical AND “&&” and logical OR “||”.

R1

R2

R1 && R2

R1 || R2

! R1

T

T

T

T

F

T

F

F

T

F

F

T

F

T

T

F

F

F

F

T

Example

If you want to assign the maximum values of i and j to k then you can write the statement

k = ( i>j ) ? i : j;

If i > j then k will get the value equal to i, otherwise it will get the value equal to j.

The general form of the ternary operator is:

(expr 1) ? expr2 : expr3

If expr1 returns true then the value of expr2 is returned as a result; otherwise the value of expr3 is returned.

Incremental operators are used to increment or decrement value of variable. Incremental operators can be applied only to variables as prefix or postfix:

i = 3;

j =4;
k = i++ + -j;

you will get the value of k as 6, i as 4 and j as 3. The order of evaluation is as follows:

1. i gets the value 3.
2. j is decremented to 3.
3. k gets the value 3 + 3.
4. i is incremented

Another important operator when programming microcontrollers is bitwise operators. Bitwise operators interprets operands as strings of bits. Bitwise operators are six: bitwise AND(&), bitwise OR(|), bitwise XOR(^), bitwise complement(~), left shift(<<), and right shift(>>). For instance:

x >> 3 causes the variable x to be shifted to the right by three bits prior to its use.

r = r | 0x0c; The hexadecimal number 0x0c is a turned on and all other bits turned off.

Operator precedence rules

Operators

Order of evaluation

Remarks

[] ( ) ->

Left to right

Array subscript, function call

− + sizeof( ) ! ++ −−

& * ~ (cast)

Right to left

Unary

* / %

Left to right

Binary Multiplicative

+ –

Left to right

>> <<

Left to right

Shift operators

< <= > >=

Left to right

Relational operators

== !=

Left to right

Equality operators

&

Left to right

Bitwise AND operator

^

Left to right

Bitwise XOR operator

|

Left to right

Bitwise OR operator

&&

Left to right

Logical And operator

||

Left to right

Logical Or operator

?:

Left to right

Conditional operator

= += -= *= /= %=

&= -= |= <<= >>=

Right to left

Assignment

,

Right to left

Comma

This site uses Akismet to reduce spam. Learn how your comment data is processed.