Select Page

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions.

Example:

Where, =, +,* are operators, a,b,c are the variables and  5 is the constants.

There are various types of the operator in C Language.

Types of Operator

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
• Increment/Decrement operators

Arithmetic Operators

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

 Operator Example (int a=9, b=2) Result + A+B 11 – A-B 7 * A*B 18 / A/B 4.5 % A%4 1

Example:

Output:

Relational Operators

Relational operators are used to compare the value of two variables.

 Operator Description > x > y (x is greater than y) < x < y (x is less than y) >= x >= y (x is greater than or equal to y) <= x <= y (x is less than or equal to y) == x == y (x is equal to y) != x != y (x is not equal to y)

Let us consider an example

Output

Logical Operators

Logical operators are used to perform logical operations on the given two variables. There are 3 logical operators in C LanguageAND(&&), OR ( || ) and NOT (!).

 Operator Description && It returns true when both conditions are true. || It returns true while at least one condition is true. ! It reverses the state of the operand.

Let us consider an example

Output

Assignment operators

Assignment operators are used to assign the values for the variable.

 Operator Description = sum = 10; 10 is assigned to variable sum += sum += 10; This is same as sum = sum + 10 -= sum -= 10; It is same as sum = sum – 10 *= sum *= 10; It is same as sum = sum * 10 /= sum /= 10; It  is same as sum = sum / 10 %= sum %= 10; It  is same as sum = sum % 10 &= sum&=10; It  is same as sum = sum & 10 ^= sum ^= 10; It is same as sum = sum ^ 10

Let us consider an example

Output

Operator Precedence in C

The precedence of operators are used to specify the operators which will be evaluated first and next.

Example:

The precedence and associatively of C operators is given below:

 Category Operator Associativity Postfix () [] -> . ++ – – Left to right Unary + – ! ~ ++ – – (type)* & sizeof Right to left Multiplicative * / % Left to right Additive + – Left to right Shift <<>> Left to right Relational <<= >>= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma , Left to right

Miscellaneous operators in c

There are few other important operators including sizeof and ?: supported by C Language.

 Operator Description sizeof() It returns the size of a variable. & It returns the address of a variable. * It assigns pointer to a variable. ? : It is a Conditional Expression.

Bitwise operators

Bitwise operator is used to perform bit by bit operations on two variables.

There are following truth table for &, | and ^.

 P q p & q p | q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1

 Operator Description &   Binary AND Operator It copies a bit to the result if it exists in both operands. |   Binary OR Operator It copies a bit if it exists in either operand. ^   Binary XOR Operator It copies the bit if it is set in one operand but not both. ~   Binary Ones Complement Operator It is unary and has the effect of ‘flipping’ bits. <<  Binary Left Shift Operator The left operands value is moved left by the number of bits specified by the right operand. >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

Increment / decrement operator

Increment Operator:

The increment operator is used to increase the value of the variable by one.

Syntax:

Example:

Decrement Operator:

Decrement operator is used to decrease the value of the variable by one.

Syntax:

Example:

Difference between Increment & Decrement operators

There are various differences between pre/Post increment & decrement operators in C.

 Operator Operator/Description Pre increment operator (++i) The value of i is incremented before assigning it to the variable i. Post increment operator (i++) The value of i is incremented after assigning it to the variable i. Pre decrement operator (- -i) The value of i is decremented before assigning it to the variable i. Post decrement operator (i–) The value of i is decremented after assigning it to variable i.