C - Operators

Operators: Operators are symbols which perform arithmetic or logical manipulation on a value or a set of values.

Operands: Operands are values on which operators are perform some operation.

Expression: An expression is either a constant, or a variable, or an array element, or a function call or a combination of such entities interconnected by one or more operators.

Different types of operators:

  1. Arithmetic Operators
  2. Unary Operators
  3. Relational Operators
  4. Equality Operators
  5. Logical Operators
  6. Assignment Operators
  7. Conditional Operators
  8. Bitwise Operators

1) Arithmetic Operators:

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. There are five arithmetic operators in C. they are as follows(Assume variable A holds 10 and variable B holds 20 then):

+AdditionA + B = 30
-SubtractionA − B = -10
*MultiplicationA* B = 200
/DivisionB / A = 2
%Remainder after integer division (Modulus operators)B % A = 0

Note:There is no exponential operator in C. However there is a library function pow() to carry out exponential.

Following are the rules associated with the use of arithmetic operators:

a)The operands acted upon by arithmetic operators must represent numeric values. Thus the operands can be integer quantities, floating-point quantities or characters.

b)The remainder operators(%) requires that the both the operands be integers and the second operands be non-zero.

c) The division operator(/) requires that the second operand be non-zero, though the operands need to be integers.

Operator precedence:

a) Precedence refers to the order in which operators in C are evaluated in an expression. Operations with higher precedence are carried out before operations having a lower precedence.

b) The operators within C are grouped hierarchically according to their precedence.

c) Arithmetic operators *,/ and % fall into one precedence group and + and - fall into another. The first group(*,/,%) has a higher precedence than the second. Thus multiplication, division and remainder operations will be carried out before addition and subtraction.

For example, the values of expression 2+3*4 will be 14 and not 20. since multiplication(*) has a higher precedence than plus(+) and will be carried out first. 12 and not 3 since division(/) will be carried out first.

Associativity :

1) Associativity is the order in which operations within the same precedence group are carried out.
2)The associativity of arithmetic operators is left-to-right.
In other words, consecutive addition and subtraction operations are carried out left-to-right, as are consecutive multiplication, division and remainder operations.
For example, the value of expression 24/4*2 will be 12 and not 3 since division(/) will be carried out first.

Unary Operators:

1) A unary operator is an operator which acts on a single operand.
2)The operator usually precedes the operand.
3)Unary operators are as follows:

a) Unary minus(-)
b) Increment operator
c) Decrement operators(--)
d) Size of operator
e) (type) Operator
f) Logical negation operator(!)

Purpose of each unary operator:
a) Unary minus(-) always precedes a numerical constant or a variable or an expression. It negates the value of the arithmetic expression which it precedes.
b) Increment operators(++) causes its operand to be increased by one.
c) Decrement operators(--) causes its operand to be decreased by one.
d) sizeof operator always precedes its operand and returns the size of its operand in bytes.
e) (type) operator is used to convert value of an expression to a different data type.
f) Logical negation operator(!) negates the value of a logical expression i.e. it causes expression that is originally true to become false and vice-versa.

Relational operators:

The four relational operators in C are as follows:

<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to

They can be used with operands which represent numeric values. Hence they cannot be used with strings.The resulting expression will be of type integer. if the value of this expression is 1 then the expression is interpreted as true and if 0 then as false.

Equality operators:

The two equality operators in C are as follows:

==Equal to
!=Not equal to

As their name suggests, equal to (==) operator checks whether two operands are equal or not equal to (!=) operator checks whether the two operands are not equal.

Logical operators:

The two logical operators in c are as follows:


&& and || operators are called logical and logical or respectively. Together they are also known as logical connectives.
2. The logical operators act upon operands that are themselves logical expressions. The net effect is to combine the individual logical expressions into more complex conditions that are either true or false.
3. The purpose of the logical and (&&) is to make the combined logical expressions true only if both the operands are true. For logical or(||), the combined expression is true if either operand is true or if both the operands are true.
4. The type of expression obtained is again a logical expression whose value is either true or false.

Assignment operators:

1. Assignment operators are used to form assignment expressions which assign value of an expression to an identifier.
2. The most commonly used assignment operator is ‘=’. Assignment expressions that make use of this operator are written in the form identifier = expression where identifier generally represents a variable and expression represents a constant, a variable or a complex expression.
3. C contains the following five additional assignment operators(Assume a=10 and b=20):

Assignment operatorsExampleResult
+-a+=b (Same as a= a+b)a=30
-=a-=b (Same as a= a-b)a=-10
a*=b (Same as a=a*b)a=200
/=a*/b (Same as a=a/b)a=1/2
%=a*%b (Same as a=a%b)a=0

4. To see how they are used, consider the operator ‘+=’. The assignment expression identifier += expression is equal to identifier = identifier +expression
5. If two operands in an assignment expression are of different data types, then the value of the expression on the right will automatically be converted to the type of the identifier on the left.
6. Assignment operators have the lowest precedence among all operators. moreover, assignment operations have right-to-left associativity.

Conditional operator:

1. Instead of an if-else statement a condition operator can be used to form a condition expression.
2. A condition expression is of the form expression 1? expression 2:expression.
3. When evaluating a conditional statement, expression 1 is evaluated first. If expression 1 is true(i.e. if its value is Non-zero), then expression 2 is evaluated and this becomes the value of the condition expression.However, if expression 1 is false(i.e. if its value is zero), then expression 3 is evaluated and this becomes the value of the conditional expression.
2. In the table given above unary operators have the highest precedence and assignment operators have the lowest precedence. thus the table mentions all the operators in the decreasing order of precedence.

Bitwise operators:

1. The once's operator (~) is a unary operator which always precedes its operand.
2.It causes bits of its operand to be inverted (i.e. reversed) so that 1's become 0's and 0's become 1's.
3.The operand must be integer type quantity(i.e. int,long,short,unsigned,char,etc.). Generally the operand is an unsigned octal or an unsigned hexadecimal quantity ,through this is not a firm requirement.
4.The one's complement operator (~) is sometimes also called complementation operator.
5. Its precedence and associativity (right to left) are same as other unary operators.

Logical Bitwise operators:

1.C support logical bitwise operators : bitwise and (&), bitwise exclusive or (^) and bitwise or (|).
2.Each of these operators requires two integer operands (i.e. int,long,short,unsigned,char,etc.) Generally the operand is an unsigned octal or an unsigned hexadecimal quantity, through this is not a firm requirement.
3.These operator work on their operand bit by bit starting from the least significant bit (i.e. the rightmost bit )and moving toward the most significant bit (i.e. leftmost bit).
4.The result of bitwise and (&) operation is 1 if both the bits have a value of 1, otherwise it is 0.
5.The result of bitwise or(|) operation is 1 if at least one of two bits has a value of 1, otherwise it is 0.
6. The result of bitwise exclusive or (^) operation is 1 if one of the bits has a value of 1 and other bit has a value of 0 otherwise it is 0. In other words, the operation returns 1 only when both the bits are different.
7.The bitwise and (&) has a highest precedence ,followed by bitwise exclusive or(^) and bitwise or(|) . The associativity for each bitwise operator is left to right.
8.The following table summarizes the result of these operation. In this table , b1 and b2 represent the corresponding bits within the first and second operand respectively .

b1b2b1 & b2b1 | b2b1 ^ b2

  • Print Page