## Operators & Expressions

- The symbols which are used to perform logical and mathematical operations in a C program are called C operators.
- These C operators join individual constants and variables to form expressions.
- Operators, functions, constants and variables are combined together to form expressions.
- Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.

## Types Of Operators

- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bit wise operators
- Conditional operators (ternary operators)
- Increment/decrement operators
- Special operators

## Arithmetic Operators

C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs.

## Example On Arithmetic Operators

## Output

## Assignment Operators

- In C programs, values for the variables are assigned using assignment operators.
- For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
- There are 2 categories of assignment operators in C language. They are,

1. Simple assignment operator ( Example: = )

2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )

## Example On Assignment Operators

## Output

## Relational Operators

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.

## Program On Realational Operators

## Output

## Logical Operators

- These operators are used to perform logical operations on the given expressions.
- There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).

## Program On Logical Operators

## Output

- In this program, operators (&&, || and !) are used to perform logical operations on the given expressions.
**&& operator**– “if clause” becomes true only when both conditions (m>n and m! =0) is true. Else, it becomes false.**|| Operator**– “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It becomes false when none of the condition is true.**! Operator**– It is used to reverses the state of the operand.- If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!” operator.
- So, “! (m>n and m! =0)” returns false (0).

## Bit Wise Operators

- These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
- Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >> (right shift).

Consider x=40 and y=80. Binary form of these values are given below.

x = 00101000

y= 01010000

All bit wise operations for x and y are given below.

- x&y = 00000000 (binary) = 0 (decimal)
- x|y = 01111000 (binary) = 120 (decimal)
- ~x = 11111111111111111111111111 11111111111111111111111111111111010111 = -41 (decimal)
- x^y = 01111000 (binary) = 120 (decimal)
- x << 1 = 01010000 (binary) = 80 (decimal)
- x >> 1 = 00010100 (binary) = 20 (decimal)

## Note

**Bit wise NOT :**Value of 40 in binary is 00000000000000000000000000000000 00000000000000000010100000000000. So, all 0’s are converted into 1’s in bit wise NOT operation.**Bit wise left shift and right shift :**In left shift operation “x << 1 “, 1 means that the bits will be left shifted by one place. If we use it as “x << 2 “, then, it means that the bits will be left shifted by 2 places.

## Program On Bit Wise Operators

## Output

## Conditional (Or) Ternary Operator

- Conditional operators return one value if condition is true and returns another value is condition is false.
- This operator is also called as ternary operator.

## Syntax

(Condition? true_value: false_value);

## Example For Ternary Operator

(A > 100 ? 0 : 1);

- In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.

## Program On Ternary Operator

## Output

## Increment (or) Decrement Operator

- Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.

## Syntax

Increment operator: ++var_name; (or) var_name++;

Decrement operator: – -var_name; (or) var_name – -;

## Example For Increment Operator

Increment operator : ++ i ; i ++ ;

## Example For Decrement Operator

Decrement operator : – – i ; i – – ;

## Program On Increment Operator

## Output

## Program On Increment Operator

## Output

## Program On Decrement Operator

## Output

## Difference b/w Pre/Post Increment/Decrement Operators

Below table will explain the difference between pre/post increment and decrement operators in C programming language.

## Special Operators

Below are some of the special operators that the C programming language offers.

In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to. Please refer **C – pointer** topic to know more about pointers.

## Program On & and * Operators

## Output

## Comma Operator

Comma as an operator:

The comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.

## Program On Comma Operator

## Output

## Operators Precedence

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.