Chapter 2
Chapter 2
OPERATORS AND
EXPRESSIONS
EXPRESSIONS
An expression in C is made up of one or more operands.
The simplest form of an expression consists of a single
operand. For example, 3 is an expression that consists of a
single operand i.e. 3. Such an expression does not specify
any operation to be performed and is not meaningful.
For example,
a=2+3 // meaningful expression
involves three operands :a, 2 and 3
two operators i.e. = (assignment operator) and +
(arithmetic addition operator).
Thus, an expression is a sequence of operands and operators
that specifies the computation of a value.
OPERANDS
An operand specifies an entity on which an
operation is to be performed.
An operand can be a variable name, a constant, a
function call or a macro name.
For example, a=printf(“Hello”)+2 is a valid
expression involving three operands namely,
• a variable name i.e. a,
• a function call i.e. printf(“Hello”) and
• a constant i.e. 2.
OPERATORS
An operator specifies the operation to be applied to its
operands.
For example, the expression:
a=printf(“Hello”)+2;
involves three operators namely,
function call operator i.e. ()
arithmetic addition operator i.e. +
assignment operator i.e. =.
BASED ON BITWISE
ROLE OF OPERATORS
OPERATOR
ASSIGNMENT
OPERATORS
MISCELLANEOUS
OPERATORS
UNARY OPERATORS
Unary operator operates on only one operand.
For example, in the expression -3,
- is unary minus operator as it operates on only one operand i.e. 3.
The operand can be present towards the right of unary operator, as
in -3 or towards the left of unary operator, as in the expression a++.
Examples of unary operators are:
1. & (address-of operator)
2. sizeof operator
3. ! (logical negation)
4. ~ (bitwise negation)
5. ++ (increment operator)
6. -- (decrement operator) etc.
BINARY OPERATORS
Binary operator operates on two operands. It requires an operand
towards its left and right.
For example, in expression 2-3
- is acting as binary minus operator as it is operating on two
operands i.e. 2 and 3.
Examples of binary operators are:
1. * (multiplication operator),
2. /(division operator),
3. << (left shift operator),
4. == (equality operator),
5. && (logical AND),
6. & (bitwise AND) etc.
TERNARY OPERATORS
Ternary operator operates on three operands.
Conditional operator (i.e. ?:) is the only ternary
operator available in C.
ARITHMETIC OPERATORS
The arithmetic operations like addition, subtraction, multiplication, division etc. can be
performed by using arithmetic operators. Following arithmetic operators are available in C:
The operators within a row have the same precedence and the order in which they
are written does not matter.
KEY POINTS: Arithmetic operators
The parenthesized sub-expressions are evaluated first.
If the parentheses are nested, the innermost sub-expression is evaluated
first.
The precedence rules are applied to determine the order of application of
operators while evaluating sub-expressions.
The associativity rule is applied when two or more operators of the same
precedence appear in the sub-expression.
If the operands of a binary arithmetic operator are of different but
compatible types, C automatically applies arithmetic type conversion to
bring the operands to a common type. This automatic type conversion is
known as implicit type conversion. The result of evaluation of operator
will be of common type. The basic principle behind the implicit arithmetic
type conversion is that if operands are of different types, the lower type
(i.e. smaller in size) should be converted to a higher type (i.e. bigger in
size) so that there is no loss in value or precision. Since, a lower type is
converted to a higher type, it is said that lower type is promoted to a
higher type and the conversion is known as promotion.
arithmetic type conversions:
→ If one operand is long double, the other will be converted to long double and the
result will be long double.
→ If one operand is double, the other will be converted to double and the result will
be double.
→ If one operand is float, the other will be converted to float and the result will be
float.
→ If one of the operands is unsigned long int, the other will be converted to
unsigned long int and the result will be unsigned long int.
→ If one operand is long int and other is unsigned int then
• If unsigned int can be converted to long int, then unsigned int operand will be
converted as such and the result will be long int.
• else, both operands will be converted to unsigned long int and the result will
be unsigned long int.
→ If one of the operands is long int, the other will be converted to long int and the
result will be long int.
→ If one operand is unsigned int, the other will be converted to unsigned int and the
result will be unsigned int.
• If none of the above is carried out, both the operands are converted to int.
MODES OF BINARY ARITHMETIC
OPERATORS
S.NO. MODE OF OPERATION
1. Integer mode: Here both the operands of a binary arithmetic
operator are of integer type and the result of
operation is also of integer type. E.g.: the result of
4/3 will be 1 instead of 1.3333.
2 Floating point mode Both the operands are of floating point type and the
result is also of floating point type.
e.g.: the result of 4.0/3.0 will be 1.333333.
3 Mixed mode: If one of the operand is of integer type and other is
of floating point type, the mode of operation is said
to be mixed mode. The operand of integer type is
promoted to floating point type and the result will
be of floating point type. For example: the result of
4/3.0 will be 1.333333.
KEY POINTS: ARITHMETIC
OPERATORS
1. The unary plus operator can appear only towards the left side of
its operand.
2. The unary minus operator can appear only towards the left side
of its operand.
3. Increment operator:
The increment operator can appear towards the left side or
towards the right side of its operand. If it appears towards the
left side of its operand (e.g. ++a), it is known as pre-increment
operator. If it appears towards the right side of its operand (e.g.
a++), it is known as post-increment operator.
The increment operator can only be applied to an operand
that has a modifiable l-value. If it is applied to an operand that
does not have a modifiable l-value, there will be “L-value
required” error.
Cont…
++a or a++ is equivalent to a=a+1.
The difference between pre-increment and post-
increment lies in the point at which the value of their
operand is incremented.
I. In case of pre-increment operator, firstly the value of
its operand is incremented and then it is used for the
evaluation of expression.
II. In case of post-increment operator, the value of
operand is used first for the evaluation of the
expression and after its use, the value of the operand
is incremented.
III. Increment operator is a token i.e. one unit. There
should be no white space character between two ‘+’
symbols. If white space is placed between two ‘+’
symbols, they become two unary plus (+) operators.
Tokens are the basic building blocks of a source code. Characters are
combined into tokens according to the rules of the programming language.
There are five classes of tokens: identifiers, reserved words, operators,
separators and constants.
4. Decrement operator:
The decrement operator can appear towards the left side or towards the right side
of its operand. If it appears towards the left side of its operand (e.g. --a), it is
known as pre-decrement operator. If it appears towards the right side of its
operand (e.g. a--), it is known as post-decrement operator.
The decrement operator can only be applied to an operand that has a modifiable
l-value. If it is applied on an operand that does not have a modifiable l-value, there
will be a compilation error “L-value required”.
--a or a-- is equivalent to a=a-1.
The difference between pre-decrement and post-decrement lies in the point at
which the value of their operand is decremented.
I. In case of pre-decrement operator, firstly the value of its operand is
decremented and then used for the evaluation of the expression in which it
appears.
II. In case of post-decrement operator, firstly the value of operand is used for
the evaluation of the expression in which it appears and then, its value is
decremented.
Decrement operator is a token i.e. one unit. There should be no white
space character between two ‘-’ symbols. If white space is placed
between two ‘-’ symbols, they become two unary minus (-) operators.
5. Division operator:
– The division operator is used to find the quotient.
• The sign of the result of evaluation of division operator depends upon the
sign of both numerator as well as denominator. If both are positive, the
result will be positive. If both are negative, the result will be positive. If
either of the two is negative, the result will be negative. For example:
4/3=1, -4/3=-1, 4/-3=-1 and -4/-3=1.
6. Modulus operator:
– The modulus operator is used to find the remainder.
• The operands of modulus operator (i.e. %) must be of integer type.
Modulus operator cannot have operands of floating point type.
• The sign of the result of evaluation of modulus operator depends only
upon the sign of numerator. If sign of numerator is positive, the sign of
result will be positive else negative. For example: 4%3=1, -4%3=-1, 4%-3=1
and -4%-3=-1.
RELATIONAL OPERATORS
• Relational operators are used to compare two quantities (i.e. their
operands). There are six relational operators in C:
2. Initialization can be done only once. Assignment can be done any number
of times.
3. Qualified constant can be initialized with Qualified constant cannot be assigned
a value. a value. It is erroneous to write a=10; if
For example, const int a=10; is valid. a is a qualified constant.
MISCELLANEOUS
OPERATORS
Other operators available in C are:
• Function call operator (i.e. ())
• Array subscript operator (i.e. [])
• Member select operator
– Direct member access operator (i.e. . (dot operator or period))
– Indirect member access operator (i.e. -> (arrow operator))
• Indirection operator (i.e. *)
• Conditional operator
• Comma operator
• sizeof operator
• Address-of operator (i.e. &)
Conditional operator
Conditional operator is the only ternary operator
available in C.
S.No Operator Name of Category ary of Precedence Associativit
Operator Operators y
1. ?: Conditional Conditional Ternary Level-I R→L
operator