C language Operators and expressions

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

Another part of the 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 latter 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.

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

R1R2R1 && R2R1 || R2! R1
TTTTF
TFFTF
FTFTT
FFFFT

Next would be ternary operators’ return values based on the outcomes of relational expressions. For example, if you want to return the value of 1 if the expression is true and 2 if it is false, you can use the ternary operator.

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 incrementing or decrement the value of a 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 get 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 interpret 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 turned on and all other bits turned off.

Operator precedence rules

OperatorsOrder of evaluationRemarks
[] ( ) ->Left to rightArray subscript, function call
− + sizeof( ) ! ++ −−  
& * ~ (cast)Right to leftUnary
* / %Left to rightBinary Multiplicative
+ –Left to rightBinary Additive
>> <<Left to rightShift operators
< <= > >=Left to rightRelational operators
== !=Left to rightEquality operators
&Left to rightBitwise AND operator
^Left to rightBitwise XOR operator
|Left to rightBitwise OR operator
&&Left to rightLogical And operator
||Left to rightLogical Or operator
?:Left to rightConditional operator
= += -= *= /= %=  
&= -= |= <<= >>=Right to leftAssignment
,Right to leftComma

Leave a Reply