Operators in C
Operator: An operator is a symbol that tells the compiler to perform certain mathematical or logical calculations. Operators are used in programs to manipulate data and variables. The data items that operators act upon are called operands.
Arithmetic Operators: There are five arithmetic operators in C. there are
Operator Purpose
+ addition
- subtraction
* multiplication
/ division
% remainder after integer division
The operands acted upon by arithmetic operators must represent numeric values. Thus, the operands can be integer quantities, floating – point quantities or characters. The remainder operator (%) requires that both operands be integer and the second operand be nonzero. Similarly, the division operator(/) requires that the second operand be nonzero.
Ex:- a, b are integer variables whose values are 10,3.
Expression Value
a + b 13
a – b 7
a * b 30
a / b 3
a % b 1
The value of an expression can be converted to a different data type if desired. To do so, the expression must be preceded by the name of the desired data type, enclosed in parentheses, i.e.,
(data type) expression
This type of construction is known as a cast.
Unary Operators:
C includes a class of operators that act upon a single operand to produce a new value. Such operators are known as unary operators. The most common unary operator is unary minus, where a numerical constant, variable or expression is preceded by a minus sign.
Ex:- -743 -0.2 -0X7FFF -(x + y)
There are two other commonly used unary operators: The increment operator, ++, and the decrement operator, - -. The increment operator causes its operand to be increased by 1, whereas the decrement operator causes its operand to be decreased by 1. The operand used with each of these operators must be a single variable.
Ex:- The expression ++ i which is equivalent to writing i= i+ 1.
A prefix operator first add one or subtract one to the operator and then the result assigned to the variable. A postfix operator first assigns the value to the variable and then increment or decrement to the operand.
Ex:- a=10;
b=++a;
The result of the above statement is, the variable ‘a’ is increment by one and result will be assigned to ‘b’. Then ‘b’ will have the value of 11.
a=10;
b=a++;
First the value of ‘a’ will be assigned to ‘b’, after that ‘a’ will be incremented by one. Then ‘b’ will have the value of 10.
a=10; a=10;
b=--a; b=a--; c=b++;
Result b = 9. Result c = 10.
Relational and logical operators:
There are four relational operators in C. They are
Operator Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
These operators all fall within the same precedence group, which is lower than the arithmetic and unary operators. The associativity of these operators is left to right.
Equality operators:
Operator Meaning
== equal to
!= not equal to
The equality operators fall into a separate precedence group, beneath the relational operators. These operators also have a left – to – right associativity.
These six operators are used to form logical expressions, which represent conditions that are either true or false. The resulting expressions will be of type integer, since true is represented by the integer value 1 and false is represented by the value 0.
In addition to the relational and equality operators, C contains two logical operators (also called logical connectives). They are
Operator Meaning
&& and
|| or
These operators are referred to as logical and and logical or, respectively. The logical operators act upon operands that are themselves logical expressions. The result of a logical and operation will be true only if both operands are true, whereas the result of a logical or operation will be true if either operand is true or if both operands are true. In other words, the result of a logical or operation will be false only if both operands are false.
C also includes the unary operator ! that negates the value of a logical expression; i.e., it causes an expression that is originally true to become false, and vice versa. This operator is referred to as the logical negation (or logical not) operator.
Operator precedence are summarized below, from highest to lowest.
Operator category Operators Associativity
Unary operator - ++ -- ! sizeof (type) RàL
Arithmetic multiply, divide
and remainder * / % LàR
Arithmetic and subtract + - LàR
Relational operat