Fundamentals - 3
Fundamentals - 3
Introduction
Operator: Operator is a special character (or symbol) that performs an operation using operands.
Operand: Operand is a variable or constant that acts as input for an operation to be performed.
Expression: Expression is a collection of operators and operands that, when evaluated, returns a value.
Ex: a+b is an expression in which the operator is + and the operands are a and b.
Unary operator: Unary operator is an operator that performs operation using only one operand at a
time.
e.g., ++ (increment) is a unary operator.
Binary operator: Binary operator is an operator that performs operation using two operands at a time.
e.g., > (greater than) is a binary operator.
Ternary operator: Ternary operator is an operator that performs operation using three operands at a
time.
e.g., ?:(Conditional operator) is ternary operator.
Here, Operand_1 (also called as LValue) can be a variable or constant. Operand_2 (also called as RValue)
can also be a variable or constant or an expression.
3
http://pradeeppearlz.blogspot.com/ Operators and Expressions
real-arithmetic expression. If both are of different values, then that expression is called as
mixed-mode arithmetic expression.
When binary operations are applied to numeric arguments of different types, numeric promotion
is performed before the operation takes place. The numeric promotion consists of converting the values of
the operands to a common type.
The rules for this type conversion are as follows:
❖ If one of the operands is a long double, then the other is converted a long double.
❖ Otherwise, if one of the operands is a double, then the other is converted to a double.
❖ Otherwise, if one of the operands is a float, then the other is converted to a float.
❖ Otherwise, if one of the operands is a long, then the other is converted to a long.
❖ Otherwise, if one of the operands is unsigned int, the other is converted to unsigned to int.
Ex: 2.3 (a double value) +3 (an int value) =5.3 (a double result)
The integer value in this expression gets promoted to double; addition is performed and the result is of
type double is returned.
Program #2
Note:
1. The operator % does not work on the operands of data types: float or double or long double.
4
http://pradeeppearlz.blogspot.com/ Operators and Expressions
2. An unusual aspect of programming in C is the arithmetic which can be done with characters.
Consider the following:
char ch1,ch2;
ch1=’a’; /*assign ‘a’ to ch1*/
ch2=ch1+1; /*assign ‘b’ to ch2*/
The second assignment increases the ASCII (American Standard Code for Information Interchange) value
for the given character, a numeric value, by 1 and sets the result to ch2. Thus, ch2 contains ‘b’.
3. x%y is always equals to (x-(x/y)*y).
4. The second operand of operators % and / must be non-zero.
Program #3
OBSERVABLE
O
U
T
P
U
5
http://pradeeppearlz.blogspot.com/ Operators and Expressions
T
6
http://pradeeppearlz.blogspot.com/ Operators and Expressions
5.2.3. Unary arithmetic operators
Unary Arithmetic operators perform the arithmetic operations such as increment & decrement and change
of sign on only one operand. There are 4 unary arithmetic operators:
Operator Operation
Unary + (Positive) Keeps the sign of value unchanged.
Unary - (Negative) Changes the sign of value.
++ (Increment) Adds 1 to value of operand and assigns the result to that operand only.
-- (Decrement) Subtracts 1 from the value of operand and assigns the result to that operand
only.
Both sign operators should be preceded by the operand where as the increment and decrement operators
can be preceded or followed by the operand.
Ex: int a=10;
1) +a /*keeps the sign of 10 as positive only*/
2) –a /* changes the sign of 10 to negative*/
3) ++a or a++ /* equivalent to a=a+1; hence, a holds 11*/
4) –-a or a-- /* equivalent to a=a-1; hence, a holds 9*/
Program #4
7
http://pradeeppearlz.blogspot.com/ Operators and Expressions
Few words about increment and decrement operators:
If ++ is used before the operand, then this increment operator is called as pre-increment operator. If
++ is used after the operand, then this increment operator is called as post-increment operator. The
same treatment will be given to the --.
The increment / decrement operator works as a simple increment / decrement by one, if it is not
used as a part of an expression. In that case, there is no difference between operations of pre or post
increment or decrement operators.
However, when these operators are used as a part of an expression then the difference can be
noticed:
● The pre-increment operator first increments the value of operand and then returns that
incremented value.
● The post-increment operator first returns the existing value of operand and then increments the
value of the operand.
● The pre-decrement operator first decrements the value of operand and then returns that
decremented value.
● The post-decrement operator first returns the existing value of operand and then decrements the
value of the operand.
8
http://pradeeppearlz.blogspot.com/ Operators and Expressions
OBSERVABLE
O
U
T
P
U
T
9
http://pradeeppearlz.blogspot.com/ Operators and Expressions
<= (is lesser than or Checks whether first operand is lesser or equals to the second operand.
equals to)
Operator Operation
10
http://pradeeppearlz.blogspot.com/ Operators and Expressions
&& (Logical AND) Performs Logical AND operation on two operands.
|| (Logical OR) Performs Logical inclusive OR operation on two operands.
! (Logical NOT) Reverses the value of operand.
11
http://pradeeppearlz.blogspot.com/ Operators and Expressions
The logical operators are used to form logical expressions as shown below:
1) Logical AND expression:
Where Expression_1 and Expression_2 are any expression those return either true or false.
When logical AND operator is encountered, first Expression_1 is tested. It returns either true or
false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions’
values are combined to give the value of logical expression as given below:
Expression_1 Expression_2 Expression_1 && Expression_2
1 1 1
1 0 0
0 1 0
0 0 0
Ex: int a=10,b=30,c;
c=((a>b)&&(a!=b)); Ans: c=1
Note: If the left operand yields false value, the right operand is not evaluated by a compiler in a logical
expression using &&.
2) Logical OR expression:
Where Expression_1 and Expression_2 are any expression those return either true or false.
When logical AND operator is encountered, first Expression_1 is tested. It returns either true or
false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions’
values are combined to give the value of logical expression as given below:
Expression_1 Expression_2 Expression_1 || Expression_2
1 1 1
1 0 1
0 1 1
0 0 0
Ex: int a=10,b=30,c;
c=((a>b)||(a!=b)); Ans: c=1
Note: If the left operand yields true value, the right operand is not evaluated by a compiler in a logical
expression using ||.
3) Logical NOT expression:
12
http://pradeeppearlz.blogspot.com/ Operators and Expressions
When logical NOT operator is encountered, first the Expression_1 is tested. It returns either true or
false. Upon the value of this Expression_1, Logical NOT returns the value given below:
Expression_1 !(Expression_1)
1 0
0 1
Ex: int a=20,b=30,c=40,d;
d=!(a>b&&b>c); ans: d=1
OBSERVABLE
O
U
T
P
U
T
13
http://pradeeppearlz.blogspot.com/ Operators and Expressions
5.2.6. Conditional operator
The conditional operator consists of two symbols: the question mark (?) and the colon (:). This is the only
operator in C that acts on three operands at a time. Hence, this operator is also called as ternary operator
or trinary operator. This operator acts in the same way as the if…else statement acts. Conditional
operator can be used as follows:
In this syntax,
Condition is any expression to be tested and it returns either true or false. Usually, it is either a relational
expression or compound relational expression.
Expression_1 and Expression_2 are any statements that are to be executed.
When the conditional operator is encountered, first, the Condition is tested. It returns either
true or false. If the condition is true, Expression_1 is executed. If the condition is false, Expression_2 is
executed. This is shown as in the following chart:
Program #4
14
http://pradeeppearlz.blogspot.com/ Operators and Expressions
15
http://pradeeppearlz.blogspot.com/ Operators and Expressions
OBSERVABLE
O
U
T
P
U
T
Bitwise operators perform operations on bits of data. These operators are used for testing, complementing
or shifting bits to the right or left. Usually, bitwise operators are not useful in cases of float and double.
There are six bitwise operators as shown below:
Operator Operation
& (Bitwise AND) Returns 1 if both corresponding bits are 1; otherwise 0.
| (Bitwise inclusive OR) Returns 0 if both corresponding bits are 0; otherwise 1.
^(Bitwise exclusive OR) Returns 0 if both corresponding bits are 0 or 1; otherwise 1.
~ (One’s complement) Returns 1, if bit is 0 and returns 0, if bit is 1.
<< (Left shift) Shifts least significant bit by specified number of times.
>> (Right shift) Shifts most significant bit by specified number of times.
All these operators are binary operators except one’s complement operator. One’s complement operator is
a unary operator.
16
http://pradeeppearlz.blogspot.com/ Operators and Expressions
Bitwise AND (&):
The operator & performs a bitwise AND between two operands. It compares each bit of the left operand
with the corresponding bit of the right operand. For each bit, the result is 1, if both the compared bits are
1; otherwise, the result is 0 as shown below:
Interview question #1
17
http://pradeeppearlz.blogspot.com/ Operators and Expressions
Bitwise Exclusive OR (^)
The operator ^ performs a bitwise exclusive OR between two operands. It compares each bit of the left
operand with the corresponding bit of the right operand. For each bit, the result is 0, if both the compared
bits have the same value; otherwise, the result is 1 as shown below:
18
http://pradeeppearlz.blogspot.com/ Operators and Expressions
Ex: int a=10;
x=~a;
Interview question #2
Program #5
Program #6
19
http://pradeeppearlz.blogspot.com/ Operators and Expressions
Examples:
20
http://pradeeppearlz.blogspot.com/ Operators and Expressions
5.2.8. Special operators
1. sizeof operator.
2. comma operator.
3. short-hand operators.
4. Referencing and dereferencing operators.
sizeof operator: It is an unary operator. It returns the number of bytes occupied by the operand in
memory. The operand may be a variable, a constant, an expression or simply a datatype.
Ex: printf(“%d”,sizeof(10)); /*prints 2 or 4*/
printf(“%d”,sizeof(int)); /*prints 2 or 4*/
int x=30; float y=45.355;
printf(“%d”,sizeof(y)); /*prints 4*/
printf(“%d”,sizeof(x+y)); /*prints 4*/
Comma Operator: A comma operator can be used as a separator or a terminator in the following
statements:
int a,b,c;
c=a,a=b,b=c;
This operator can also be used to link related expressions together. A comma-linked list of expressions
is evaluated from left to right and the value of right-most expression is the value of combined
expression.
Ex: value=(x=10,y=20,x+y); ans: value=30
Short-hand Operators: These operators are formed by combining both binary arithmetic operators
and bitwise operators with assignment operator. Hence, these are also called as compound assignment
operators.
The short-hand operators include: +=, -=, *=, /=, %=
&=, |=, ^=, <<=, >>=.
All of these operators are binary operators. So, these operands should act on two operands at a time.
21
http://pradeeppearlz.blogspot.com/ Operators and Expressions
Refencing and dereferencing operators: Referencing operator (&) is the operator that returns the
address of operand in the memory. Dereferencing operator (*) is the operator that returns the value at
the address that is pointed by the referencing operator.
Ex: int a=10;
printf(“\n Address of a=%u”,&a); /*prints address of a*/
printf(“\n Value of a=%d”,*(&a)); /*prints value of a */
O a) y=(int) (x+0.5);
b) y=int (x+0.5);
U c) y=(int )x+0.5;
d) y=(int)((int)x+0.5)
T
P
U
T
22
http://pradeeppearlz.blogspot.com/ Operators and Expressions
The following table lists the operators, their precedence and associativity.
Precedence Operands Operators Associativity
1 2 () [] . -> Left-to-Right
2 1 ! ~ ++ -- + - * & Right-to-Left
(type) sizeof
3 2 * / % Left-to-Right
4 2 + - Left-to-Right
7 2 == != Left-to-Right
8 2 & Left-to-Right
9 2 ^ Left-to-Right
10 2 | Left-to-Right
11 2 && Left-to-Right
12 2 || Left-to-Right
13 3 ? : Left-to-Right
14 2 = *= /= %= += -= Right-to-Left
&= |= ^= <<= >>=
15 2 , Left-to-Right
23
http://pradeeppearlz.blogspot.com/ Operators and Expressions
24
http://pradeeppearlz.blogspot.com/ Operators and Expressions
5.3. Type conversion in an expression
Type conversion is the process of converting a value from one data type to another. This type conversion
makes the operands in an expression belong to similar data type.
If casting is done from lower size type to higher size type value, then it is called as broadening
conversion. E.g., char to int.
If casting is done from higher size type to lower size type value, then it is called as narrowing
conversion, causes some loss of value.
E.g. float to int causes truncation of fractional part.
float to long causes truncation of fractional part.
double to float causes rounding of digits.
long int to int causes dropping of the excess high order bits.
int to short causes dropping of the excess high order bits.
Note: If the two operands in an assignment statement are of different data types, the right side operand
is automatically converted to the data type of the left side.
Ex: int a=10; Ex: float a=10.98;
float b; int b;
b=a; b=a;
printf(“%f”,b); printf(“%d”,b);
output: output:
Program #4
26
http://pradeeppearlz.blogspot.com/ Operators and Expressions
OBSERVABLE
O
U
T
P
U
T
Explicit type conversion: When we want to do type conversion forcefully, then we use cast operator.
Cast operator is used to convert value of an operand from one type to another explicitly by the user. This
explicit type conversion is also called as casting or coercion.
The usage of cast operator is as follows:
Here, type_name is any standard data type. The expression may be a constant, variable or an expression.
As this operator works on only one operand at a time, this is a unary operator.
5.6. Conclusion
C supports arithmetic, relational and logical operators like other programming languages. It supports
increment and decrement operators for faster execution. Bitwise operators are supported in C, which are
not available in other languages. In addition to simple assignments, C also provides compound
assignments or short-hand assignments.
Expressions are formulated with the help of operators and operands. These are evaluated to get
the desired result. These are evaluated according to the precedence levels of the operators and their
associativity. In evaluating mixed-type expressions, implicit conversion rules are followed. Explicit type
conversions are possible with coercion or type casting.
27
http://pradeeppearlz.blogspot.com/ Operators and Expressions