Operators and Expressions

An operator is a symbol that ‘operates’ one or more expression producing a value that can be assigned to the variable. The operations are represented by operators and the objects of operation are referred as operands. Some operators require two operands is known as binary operators and some operators require only one operands is known as unary operators. C language is rich in builtin operators that fall under several different categories are:
  • Arithmetic operators
  • Assignment operators
  • Increments and Decrements operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Precedence and Associativity operators

Arithmetic operators

Arithmetic operators are used for numeric calculations. C supports two unary “+ , – ” and five binary arithmetic operators ” + , – , * , / , %. The following table show the all arithmetic operators. Lets take two variable a=10 and b=5.
Operators Descriptions Examples
+ Adds the two operand. a + b = 15
Subtracts the second from the first one. a – b = 5
* Multiple the both operand a * b = 50
/ Divides the first operand with the second operand. a / b = 2
% Modulus operator and give the remainder in integer division. a % b = 0
Unary + Unary + must arithmetic or pointer type and result is the value of the argument. if x=10 then +x mean 10
Unary – Unary – must arithmetic or pointer type and result is negation of the operand value. if x=10 then -x mean -10
In the examples, – changes the sing of the operand x. The unary + and unary- operators are different form the addition and subtraction operators.

Assignment operators

A value can be stored in a variable using assignment operators. The following table show the all assignment operators supported by C.
Operators Descriptions Exampls
= Simple assignment operator. The value of right side operand is assigned to left side operand. x = 5 5 is assigned to x
+= The value of left side operand will added to the value of right side operand and assign the result to the left side operand. x += 5 is equivalent to x = x + 5
-= The value of right side operand will subtracted from the value of left side operand and assign the result to the left side operand. x -= 5 is equivalent to x = x – 5
*+ The value left side operand will multiplied by the value of right side operand and assign the result to the left side operand. x *= 5 is equivalent to x = x * 5
/= The value of left side operand will divided by the value of right side operand and assign the result to the left side operand. x /= 5 is equivalent to x = x / 5
%= The integer division is carried out between the left side operand and right side operand and assign the result to the left side operand. x %= 5 is equivalent to x = x % 5
<<= Left shift and assignment operator. x <<= 5 is same as x = x << 5
>>= Right shift and assignment operator. x >>= 5 is same as x = x >> 5
&= Bitwise AND assignment operator. x &= 5 is same as x = x & 5
^= Bitwise exclusive Or assignment operator. x ^= 5 is same as x = x ^ 5
|= Bitwise inclusive OR assignment operator. x |= 5 is same as x = x | 5

Increments / Decrements operators

Increments and Decrements operators are unary operators because they operate on a single operand. The following table show the increments and Decrements operators supported by C.
Operators Descriptions Examples
++ Increments operator. The increments operator increase the integer value by 1. ++x is equivalent to x = x+1
– – Decrements operator. The decrements operator decrease the integer value by 1. –x is equivalent to x = x-1

Relational operators

Relational operators are used to compare values of two expressions. If the relation is true then the vale of expression is 1 and if the relation is false then the value of expression is 0.The following table show the all operators supported by C. Lets take two variable a=9 and b=5.
Operators Meanings Expressions Relations Value if exp.
< less then a<b</b False 0
<= less than or equal to a<=b False 0
== equal to a==b False 0
!= Not equal to a!=b True 1
> Greater then a>b True 1
>= Greater then or equal to a>=b True 1

Logical operators

A logical operators used to connect two or more expression. The following table show the logical operators supported by C. Lets take variable a=10 and b=5.
Operators Meanings Descriptions Examples
&& AND give the result is true if the both condition is true. (a==10) && (a<b) = false</b)
|| OR give the result is false if the both condition is false (a==b) || (a<b) =true</b)
! NOT This is a unary operator. If the condition is false then give the result is true.If the condition is true then give the result is false. !(a==10) = false

Bitwise operators

Bitwise operators operate on integers only and they are used for operations on individual bits.C support manipulation of data at the bit level. The The following table show the all bitwise operators.
Operators Descriptions Examples
& Bitwise AND. Is require two operand and compared both operand. The resulting bit is 1 only when the bits in both operand are 1, otherwise it is 0. 0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = 1
| Bitwise OR. Is require two operand and compared both operand. The resulting bit is 0 only when the bits in both operand are 0, otherwise it is 1. 0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1
^ Bitwise XOR. Is require two operand and compared both operand. The resulting bit is 1, if bits of both operand have different value. Otherwise it is 0. 0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0
~ One’s complement. Is require only one operand and flips the value of all the bits in the operand. 1 changed to 0 0 changed to 1
<< Left shift. Is used for shifting the bits left side and require two operand. On shafting bits left, equal number of bit position on right are vacated.There position are filled are in with 0 bits. Bit x 101010010 Bit x << 3 110101000
>> Right shift. Shifts the bits to the right side. On shifting bits right, equal number of bit position in left are vacated. There position are filled are in with in 0 bits in unsigned integers. Bit x 0110100011 Bit x >> 3 0000100101

Other types of operators

C provide some other types of important operators. That are must used in C program. The following table show the operated supported by C.
Operators Descriptions Examples
& Address operator. It give the starting address of the variable name in the memory. &basic; is a variable
? : Conditional operator. It written as- TextExp ? exp 1 : exp 2. if TextExp is true, then exp 1 is evaluated. if the TextExp is false, then exp 2 is evaluated. a = 3 , b = 2 a > b ? a : b is a.
, Comma operator. Is used to permit different expressions to appear in situations where only one exp would be used. a=3, b=2, c=5
sizeof Sizeof operator. gives the size of its operand in terms of bytes. The operand can be a variable,constant or any datatype. sizeof ( int )
(datatype) Cast operator. Is used for converting expression to a particular data type temporarily. The syntax- (datatype) exp; x (float) a / b

Precedence and Associativity of operators

Evaluation of expression having more then one operator, there are certain precedence and associativity. Lets see the expression- 2 + 3 * 5 have a two operators. If addition is performed before multiplication then result will be 25. if multiplication is performed before addition then the result will be 17. In C, operators are grouped together and each group is given a precedence level. The operators with precedence level 1 have highest precedence and with level 15 have lowest precedence. The operator with a higher precedence is evaluated first. The some operators are evaluation from left to right and from right to left depending on the level. This is known as associativity property of operator.The Following table show the all precedence and associativity operators.
Operators Descriptions Precedence level Associativity
( ) [ ] . Function Array subscript Dot operator Arrow operator 1 (highest) Left to right
+ – ++ — ! * &
~
(datatype) sizeof
Unary plus Unary minus Increments Decrements Logical NOT Indirection One’s complement Type cast Size of bytes 2 Right to Left
* / % Multiplication Division Modulus 3 Left to Right
+ – Addition Subtraction 4 Left to Right
<< >> Left shift Right shift 5 Left to Right
< <= > >= Less then Less the or equal to Greater then Greater then or equal to 6 Left to Right
= = != Equal to Not equal to 7 Left to Right
& Bitwise AND 8 Left to Right
^ Bitwise XOR 9 Left to Right
| Bit wise OR 10 Left to Right
&& Logical AND 11 Left to Right
|| Logical OR 12 Left to Right
? : Conditional operator 13 Right to Left
= *= /= %= += -= &= ^= |= <<= >>= Assignment operators 14 Right to Left
, Comma operator 15 Left to Right