# Operators – C

Operators are symbols which will let the compiler know about specific operations to be executed. C language supports a variety of operators:

• Arithmetic
• Relational
• Logical
• Bitwise
• Assignment
• Other

Throughout the examples following variables will be used with the following values:

```short a = 11;
short b = 33;
short c = 0;```

Note that in C language all values which are not 0 or NULL (NULL is 0 typecasted to void*) are considered true .

## Arithmetic

The following table shows arithmetic operators supported by C.

Operator Description Example Output
`+` Add two operands `a + b` 44
`-` Subtract second operand from first `a - b` -22
`*` Multiply the operands with eachother `a * b` 363
`/` Divide first operand by the second `b / a` 3
`%` Calculates the remainder of the division (See 2) `b % a` 0
`++` Increment operands value by one `a++` 12
`--` Decrement operands value by one `a--` 10

Some things to look out for:

• `++, --` operators can be both postfix and prefix meaning that:
• Postfix operator will increment / decrement value, however in expression the old value is used
• Prefix operator will increment / decrement value, expression uses new value
• Example:
```int prefix = 10,
postfix = 10;

printf("postfix - Exp: %d\nAfter exp: %d\n",
postfix++, postfix);
printf("prefix - Exp: %d\nAfter exp: %d\n",
--prefix, prefix);```
```postfix - Exp: 10
After exp: 11
prefix - Exp: 9
After exp: 9```
• When doing arithmetic operations make sure that signedness ( See 3) will not affect your result. Meaning that variable should be able to store big enough numerical values.
• Example:
```char overflow = 120;
overflow = overflow + 10;

printf("Result: %d\n", overflow);```

`Result: -126`

## Relational

Operator Description Example Output
`==` Results in a true value when both operands have the same value `(a == b)` false
`-` Results in a true value when both operands have different values `(a != b)` true
`>` Results in a true value when first operand is greater than second operand `(a > b)` false
`<` Results in a true value when first operand is less than second operand `(a < b)` true
`>=` Results in a true value when first operand is greater than or equal to second operand `(a >= b)` false
`<=` Results in a true value when first operand is less than or equal to second operand `(a <= b)` true

## Logical

Operator Description Example Output
`&&` Logical AND operator. Logical multiplication (Only true when all operands are true) `(a && c)` false
`||` Logical OR operator. Logical addition (Only true when at least one of the operands is true) `(a || c)` true
`!` Logical NOT operator. Logical negation (true when value is false, false when value is true) `!(a && c)` true

## Bitwise

Bitwise operators perform bit-by-bit operations. C supports the following bitwise operations:

• AND – &
• OR – |
• XOR – ^
• inversion – ~
• left shift – <<
• right shift – >>

Bitwise operations are most often used with:

• Hashing functions
• Bit manipulations (Embedded systems, modifying register values)

The previously mentioned values have the following representation in binary base system:

```a = 00001011
b = 00100001```
Operator Description Example Output
`&` Bitwise AND operator. Logical multiplication (Only true when all operands are true) `(a & b)`
```00001011
00100001
--------
00000001```
`|` Bitwise OR operator. Logical addition (Only true when at least one of the operands is true) `(a | b)`
```00001011     00100001
--------
00101011```
`^` Bitwise XOR (Exclusive OR) operator. Only true when operands have different values `(a ^ b)`
```00001011
00100001
--------
00101010```
`~` Bitwise inversion – All bits are inversed (0 -> 1; 1 -> 0) `~(a)`
```00001011
--------
11110100```
`<<` Bitwise left shift – left side operand bits shifted left by the amount of the right side operand `(a << 3)`
```00001011
--------
01011000```
`>>` Bitwise left shift – left side operand bits shifted left by the amount of the right side operand `(b >> 3)`
```00100001
--------
00000100```

Truth table

x y & | ^
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

NB! Careful when making bitwise operations with signed integers (char, short, int, long, long long), be vary of signedness and how it affects the result:

• Example with bit shifting
```char a = 0b10011001;            // -103
unsigned char b = 0b10011001;   // 153
printf("a: %d 0x%x\n", a, a);   // a: 10011001
printf("b: %d 0x%x\n", b, b);   // b: 10011001
// shift right
a >>= 3;                        // sign bit will be shifted in
b >>= 3;                        // 0 will be shifted in
printf("a: %d 0x%x\n", a, a);   // a: 11110011 (0xf3)
printf("b: %d 0x%x\n", b, b);   // b: 00010011 (0x13)```
```a: -103 ffffff99
b: 153 99
a: -13 fffffff3
b: 19 13```

## Assignment

Assignment operators assign right side operands to left side operand. C supports the following assignment operands:

Operator Description Example Left side operands value
`=` Assignment operator, assigns right side operands to the left side operand `c = a * b` 363
`+=` Add AND assignment operator, assigns left side operand the value of left side operand + right side operand `a += b`
`a = a + b`
44
`+=` Subtract AND assignment operator, assigns left side operand the value of left side operand – right side operand `a -= b`
`a = a - b`
-22
`*=` Multiply AND assignment operator, assigns left side operand the value of left side operand * right side operand `a *= b`
`a = a * b`
363
`/=` Divide AND assignment operator, assigns left side operand the value of left side operand / right side operand `b /= a`
`b = b / a`
3
`%=` Modulus AND assignment operator, assigns left side operand the value of left side operand % right side operand `b %= a`
`b = b % a`
0
`<<=` Left shift AND assignment operator, assigns left side operand the value of left side operand << right side operand `a <<= 3`
`a = a << 3`
88
`>>=` Right shift AND assignment operator, assigns left side operand the value of left side operand >> right side operand `a >>= 3`
`a = a >> 3`
1
`&=` Bitwise and AND assignment operator, assigns left side operand the value of left side operand & right side operand `a &= b`
`a = a & b`
1
`|=` Bitwise or AND assignment operator, assigns left side operand the value of left side operand | right side operand `a |= b`
`a = a | b`
43
`^=` Bitwise xor AND assignment operator, assigns left side operand the value of left side operand ^ right side operand `a ^= b`
`a = a ^ b`
42

## Miscellaneous

In addition to previously mentioned operators there are more important operators in C:

Operator Description Example Sample output
`sizeof()` Size of a variable `sizeof(a);` 2
`&` Memory address of a variable. Address length depends on architecture. `&a;` `0x80fe2456`
`*` Value from memory address of a variable. Also known as defererencing.
```short *x = &a;
*x;```
11
`? :` Ternary operator, conditional expression. condition ? expression when condition is true : expression when condition is false
`c = (a == 11) ? 22 : 55;`
22
`(type)` Typecasting operator, casts variable to a different type `(int)a;` expression holds value of a with an integer size (4 bytes)

## Operator precedence

Operator precedence determines how the expression is evaluated, operands with higher precedence will be grouped together earlier.

For example as is in math, multiplication and division has higher precedence than adding and subtracting. This means that expression `y = 2 * 5 + 20 / 4` will be evaluated as `y = 10 + 5` -> `y = 15`. To specify the precedence parhenthesis should be added e.g. `y = 2 * ( 5 + 20) / 4` would be evaluated to `y = 2 * 25 / 4`-> `y = 50 / 4`-> `y = 12.5` (12 when dealing with integers). Same applies to C language.

The following table contains the operator precedence and associativity.

# Category Operator Associativity
1. Postfix `(), [], ->, ., ++, --` (>>>) Left to right
2. Unary `+, -, !, ~, ++, --, (type), *, &, sizeof()` (<<<) Right to left
3. Multiplicative `*, /, %` (>>>) Left to right
4. Additive `+, -` (>>>) Left to right
5. Shift `<<, >>` (>>>) Left to right
6. Relational `<, >, <=, >=` (>>>) Left to right
7. Equality `==, !=` (>>>) Left to right
8. Bitwise AND `&` (>>>) Left to right
9. Bitwise XOR `^` (>>>) Left to right
10. Bitwise OR `|` (>>>) Left to right
11. Logical AND `&&` (>>>) Left to right
12. Logical OR `||` (>>>) Left to right
13. Ternary `? :` (<<<) Right to left
14. Assignment `=, +=, -=, *=, /=, %=, >>=, <<=, &=, |=, ^=` (<<<) Right to left
15. Comma `,` (>>>) Left to right

Examples:
NB! These examples assume that after each line the variables will have their values reset to initialized values.

```// Variables and initialized values
short a = 11;
short b = 33;
short c = 0;
short d = -22;
short result = 0;```

In the following example some expressions are grouped by 3 lines, in which each line has the following :

1. expression
2. same expression with parenthesis added
3. same operands and variables, different parenthesis placement
```result = a >> 3 ? b++ * 5: a + b;           // result: 165

result = a << 1 + 6 / 3 + ++c;              // result: 176
result = (a << (1 + ((6 / 3) + ++c)));      // result: 176
result = ((a << 1) + (6 / (3 + ++c)));      // result: 23

result = (unsigned char)d % ++a - d;        // result: 8
result = ((((unsigned char)d) % (++a)) - d);// result: 28
result = (((unsigned char)d) % ((++a) - d));// result: 30

result = a & 0x04 && d && b;                // result: 0
result = (((a & 0x04) && d) && b);          // result: 0
result = ((a & (0x04 && d)) && b);          // result: 1```

## Resources

1. C operators [ Link ]
2. Modulus operation [ Link ]
3. Signed number representations [ Link ]

## 2 thoughts on “Operators – C”

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