# C Programming Language Operators

## Contents

### C Operators

#### Assignment Operator

Operator | Syntax | Explanation |
---|---|---|

Assignment | a = b | assign to a the value of b |

#### Arithmetic Operators

Operator | Syntax | Explanation |
---|---|---|

Addition | a + b | the value of a+b |

Subtraction | a - b | the value of a-b |

Unary plus | +a | the value of a |

Unary negation | -a | the value of -a |

Multiplication | a * b | the value of a multiplied by b |

Division | a / b | the value of a divided by b (unless both are integers when integer division is done instead) |

Modulo/Remainder | a % b | remainder of a divided by b (e.g. 4%3 is 1) |

Increment | prefix: ++a suffix: a++ |
increase a by 1 and then use the value of a use the value of a and then increase a by 1 |

Decrement | prefix: --a suffix: a-- |
decrease a by 1 and then use the value of a use the value of a and then decrease a by 1 |

#### Comparison Operators

These comparisons return true or false based on evaluation, e.g., if a is 2 and b is 2, then `a==b` is true. These can be viewed as a question like in the explanation field of the following table:

Operator | Syntax | Explanation |
---|---|---|

Equality check | a == b | is a equal to b? |

Inequality check | a != b | is a not equal to b? |

Greater than | a > b | is a greater than b? |

Less than | a < b | is a less than b? |

Greater than or equal to | a >= b | is a greater than or equal to b? |

Less than or equal to | a <= b | is a less than or equal to b? |

#### Logical operators

Logical operators express the boolean logic operators NOT, AND, OR as show in this table:

Operator | Syntax | Explanation |
---|---|---|

Logical NOT | !a | negation of a: if a is true then false; if a is false then true |

Logical AND | a && b | true if both a and b are true, otherwise false |

Logical OR | a || b | true if either a or b is true, false if both are false |

#### Bitwise operators

Bitwise operators operate on each bit within a value.

Operator | Syntax | Explanation |
---|---|---|

Bitwise NOT | ~a | logical negation on each bit |

Bitwise AND | a & b | logical AND on each bit |

Bitwise OR | a | b | logical OR on each bit |

Bitwise XOR | a ^ b | logical eXclusive OR on each bit |

Bitwise left shift | a << b | shifts bits of a by b positions left |

Bitwise right shift | a >> b | shifts bits of a by b positions right |

**NOTE:** be careful with shifting bits. There can often be undefined behaviour for shifting negative integers. Care should always be taken. One should have a firm understanding of the internal representation of binary numbers (such as two's-complement) to implement shifts.

#### Compound Assignment

Compound assignment operators do the operation and then assigned the variable. The following table shows details:

Operator | Syntax | Explanation |
---|---|---|

Addition assignment | a += b | add b to a and assign result to a (a = a+b) |

Subtraction assignment | a -= b | subtract b from a and assign result to a (a = a-b) |

Multiplication assignment | a *= b | multiply a by b and assign result to a (a = a*b) |

Division assignment | a /= b | divide a by b and assign result to a (a = a/b) |

Modulo/remainder assignment | a %= b | divide a by b and assign remainder to a (a = a%b) |

Bitwise AND assignment | a &= b | perform a&b and assign result to a (a = a&b) |

Bitwise OR assignment | a |= b | perform a|b and assign result to a (a = a|b) |

Bitwise XOR assignment | a ^= b | perform a^b and assign result to a (a = a^b) |

Bitwise left shift assignment | a <<= b | shift bits of a left by b positions and assign result to a (a = a<<b) |

Bitwise right shift assignment | a >>= b | shift bits of a right by b position and assign result to a (a = a>>b) |

#### Member operators

Operator | Syntax | Explanation |
---|---|---|

Array element/subscript | a[b] | element of array a at index b (note that first element has index 0) |

Pointer dereference | *a | value stored at location pointed to by a |

Address of (pointer to) | &a | memory address of a |

Structure/Union dereference | a->b | the member b of the structure/union pointed to by a (a must be a pointer to a structure or union) |

Structure member | a.b | the member b of the structure/union a |

#### Other operators

Operator | Syntax | Explanation |
---|---|---|

Function call | a(arg0, arg1, ..., argN) | function name followed by comma-separated list of arguments in parentheses |

Comma | (a,b) | evaluate a, then evaluate b, from left to right until each expression in the list is evaluated, then the whole expression ("(a,b)" here) evaluates to the value of the last expression ("b" here) |

Ternary conditional | a ? b : c | if a is true, then b, otherwise c (If it means something to do, this is the functional, rather than imperative, meaning of conditional.) |

Size-of | sizeof(a) | the size of a in bytes (a may be a variable or a type, but doesn't work inside function to which a is an array parameter) |

Align-of | alignof(a) | check the alignment of type a |