You are on page 1of 34

Unit 1

OPERATORS AND
EXPRESSIONS IN C

C by Dr. Deepika Bhatia 1


OPERATOR IN C LANGUAGE:-
• An operator is a symbol which operates on a value or a variable. For
example: + is an operator to perform addition.
• Manipulates data and variables.

• C programming has wide range of operators to perform various


operations. For better understanding of operators, these operators can be
classified as:

1. Arithmetic Operators (+,-,*,/,%)


2. Increment and Decrement Operators (++,--)
3. Assignment Operators (=, +=,-=,*=,/=, %=)
4. Relational Operators (>,<,>=,<=,==,!=)
5. Logical Operators (&&,||,!)
6. Conditional Operator (?:)
7. Bitwise Operators (&,|,!)
8. Special Operators (, , sizeof)
C by Dr. Deepika Bhatia 2
Expression in C

It is a sequence of operands and operators that


reduces to a single value
Example:
10+15
Value can be of any type other than void.

C by Dr. Deepika Bhatia 3


1. Arithmetic Operator

Operator Meaning of Operator


+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division (returns quotient)
% MODULO OPERATOR:-remainder after
division( returns remainder)-used for
integers

• Unary(sizeof,-,++,--,&)
• Binary(+,-,*,/,%),
• Ternary Operators,
operands are a and b in expressions a+b
C by Dr. Deepika Bhatia 4
2. Increment and Decrement Operators

1.increment ++ and decrement -- change the value of an


operand (constant or variable) by 1.
2. Increment ++ increases the value by 1 whereas decrement
-- decreases the value by 1.
3. These two operators are unary operators, meaning they
only operate on a single operand.

eg. int a=10, b=100


++a = 11
--b = 99

C by Dr. Deepika Bhatia 5


3. Assignment operator
An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =
int a=12,b=5;

Operator Example Same as


= a=b a = b // a=5
b=a b=a//b=12
Sh orthand notation

+= a += b a = a+b //a=17
-= a -= b a = a-b //a=7
*= a *= b a = a*b //a=60
/= a /= b a = a/b //a=2 ,12/5=2
%= a %= b a = a%b //a=2 , 12%5
C by Dr. Deepika Bhatia 6
4. Relational operator- there are 06 as below
A relational operator checks the relationship between two
operands.
If the relation is true, it returns 1; if the relation is
false, it returns value 0.
Relational operators are used in decision making and loops.

Operator Meaning of Operator Example


== Equal to 5 == 3 returns 0
> Greater than 5 > 3 returns 1
< Less than 5 < 3 returns 0
!= Not equal to 5 != 3 returns 1
>= Greater than or equal to 5 >= 3 returns 1
<= Less than or equal to 5 <= 3 returns 0

C by Dr. Deepika Bhatia 7


5. C Logical Operators- count 3 as: AND &&, OR || ,NOT !

Logical Operator AND (&&)


C1 && C2 =
Where C1 , C2 are conditions.

Indicates whether both operands are true or not.

The logical AND (&&) should not be confused with the bitwise AND
(&) operator. For example:
1 && 4 evaluates to 1 (True && True = True)
while
1 & 4 (0001 & 0100 = 0000) evaluates to 0//bitwise
operator

C by Dr. Deepika Bhatia 8


C Logical operators in detail
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then −

Operator Description Example

&& Called Logical AND operator. If both the operands (A && B) is false.
are non-zero, then the condition becomes true.

|| Called Logical OR Operator. If any of the two (A || B) is true.


operands is non-zero, then the condition becomes
true.

! Called Logical NOT Operator. It is used to reverse !(A && B) is true.


the logical state of its operand. If a condition is true,
then Logical NOT operator will make it false.

C by Dr. Deepika Bhatia 9


Logical AND Operators Truth Table below:
TRUE=1 C1 c2 C1&&c2
FALSE=0 F F F
F T F
T F F
T T T

if (x > 0 && y > 0)


a b a && b a || b
{
0 0 0 0 printf("Both numbers Positive.\n");
0 1 0 1 }
else
1 1 1 1
{
1 0 0 1 printf("Both numbers not positive\n");
}
}
C by Dr. Deepika Bhatia 10
Logical Operator OR (||)

Indicates whether either operand is true. If either of the


operands has a nonzero value, the result has the value 1.
Otherwise, the result has the value 0. The type of the result
is int. Both operands must have a arithmetic or pointer
type. The usual arithmetic conversions on each operand are
performed.

The logical OR (||) should not be confused with the bitwise


OR (|) operator. For example:
1 || 4 evaluates to 1 (or True || True = True)

while 1 | 4 (0001 | 0100 = 0101) evaluates to 5


C by Dr. Deepika Bhatia 11
Logical OR Operator Truth table below:
TRUE=1 c1 c2 C1||c2
FALSE=0 F F F
F T T
T F T
T T T
a b a && b a || b
0 0 0 0
0 1 0 1
1 1 1 1
1 0 0 1
if(today == Sunday || today == Saturday)
{
// It is a holiday
} C by Dr. Deepika Bhatia 12
Logical NOT Operator (unary)-toggle
!
Syntax: !Condition
// returns true if the conditions is false
// else returns false

The result has an int type. The operand must be an


integral, floating, or pointer value.
Z=!(4 == 2) //!(0) otuput is z=1
!x

C by Dr. Deepika Bhatia 13


Logical NOT Operator (unary)-Truth table below

c1 !c1
F T
T F

C by Dr. Deepika Bhatia 14


6. C Conditional Operator: The conditional operator is also known
as a ternary operator. The conditional statements are the decision-making
statements that depend upon the output of the expression. As a conditional
operator works on three operands, so it is also known as the ternary
operator.
Conditional Operator (ternary)/ short cut of- if else ? :
This operator uses 3 operands.
(condition)? statement1:statements2
The first operand/expression is evaluated, and its value determines whether
the second or third operand/expression is evaluated:
If the value is true, the second operand/expression is evaluated. If the
value is false, the third operand/expression is evaluated.The result is the
value of the second or third operand/expression. The Syntax is:
First operand ? second operand : third operand
EXAMPLE: size != 0 ? size : 0
C by Dr. Deepika Bhatia 15
Program on conditional operator
// C program to find largest among two
// numbers using ternary operator

#include <stdio.h>

int main()
{
int m = 10, n = 15;

(m > n) ? printf("m is greater"): printf("n is greater");

return 0;
}
Ouptut is: n is greater C by Dr. Deepika Bhatia 16
7. BITWISE OPERATOR
Used for manipulating data at bit level Not applied to float or double
C supports a set of bitwise operators for bit manipulation as listed in
Table 3.8. C supports six bit operators. These operators can operate
only on integer operands such as int, char, short, long

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> C by Dr. Deepika Bhatia Shift right 17
The following table lists the Bitwise operators supported by C. Assume
variable 'A' holds 60 and variable 'B' holds 13, then −

Operator Description Example


& Binary AND Operator copies a bit to the result if it (A & B) = 12, i.e., 0000 1100
exists in both operands.
| Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.
^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49, i.e., 0011 0001
operand but not both.
~ Binary One's Complement Operator is unary and has the (~A ) = ~(60), i.e,. 1100 0011
effect of 'flipping' bits.
<< Binary Left Shift Operator. The left operands value is
moved left by the number of bits specified by the right A << 2 = 240 i.e., 1111 0000
operand.

>> Binary Right Shift Operator. The left operands value is


moved right by the number of bits specified by the right A >> 2 = 15 i.e., 0000 1111
operand.

C by Dr. Deepika Bhatia 18


C by Dr. Deepika Bhatia 19
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25


00001100
& 00011001
________
00001000 = 8 (In decimal)
Example 1: Bitwise AND
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Output = 8 C by Dr. Deepika Bhatia 20
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and


12 = 00001100 (In Binary) 25
25 = 00011001 (In Binary) 00001100
^ 00011001
Bitwise OR Operation of 12 and ________
25 00010101 = 21 (In decimal)
00001100
| 00011001 Complement:
________ 35 = 00100011 (In Binary)
00011101 = 29 (In decimal)
Bitwise complement Operation of
35
~ 00100011
________
11011100
C by Dr. Deepika Bhatia = 220 (In decimal) 21
Thus, the value of 40<<1 is
01010000. The decimal
equivalent of this binary
value is 80.

C by Dr. Deepika Bhatia 22


8. SPECIAL OPERATORs in C

Comma operator: used to link related expression


together. A comma linked list of expression evaluated left to right and
the value of right most expression is the value of combined
expression
V= (x=10,y=5,x+y);
sizeof operator: it is compile time operator.
•It returns number of bytes the operand occupies.
The sizeof() operator contains a single operand which can be either
a data type or an expression
int x=20,y=5;
m=sizeof (x);
sizeof(char);
sizeof(x+y); C by Dr. Deepika Bhatia 23
C by Dr. Deepika Bhatia 24
C by Dr. Deepika Bhatia 25
Priority of operators in C

C by Dr. Deepika Bhatia 26


C by Dr. Deepika Bhatia 27
Associativity of
operators
i.E which operators to perform first.
Eg:
Left to right: 12*4/8%2
Here all operators have same precedence, so
output is 48/8%2=6%2=0 answer

Right to left: a=b=c


Here, c is assigned to b then b to a.
C by Dr. Deepika Bhatia 28
Statements and its types in C

C programs are collection of Statements, statements is an


executable part of the program it will do some action. In general
all arithmetic actions and logical actions are falls under
Statements Categories anyway there are few Statement categories
1. Expression Statements.
2. Compound Statements.
3. Selection Statements.
4. Iterative Statements.
5. Jump Statements.
C by Dr. Deepika Bhatia 29
Expression 0Statements: It is combination of
variables,Constants,operators,Function Calls and
followed by a semicolon. Expression can be any
operation like Arithmetic operation or Logical Operation.
Few Examples for expression Statements

X = Y + 10 ;
20 > 90;
a?b:c;
a = 10 + 20 * 30;
; (This is NULL Statement ).

C by Dr. Deepika Bhatia 30


Compound statement in C

Compound statement is combination of several expression


statements. Compound Statement is Enclosed within the Braces { }.
Compound statement is also called as Block Statement.
There is no need of any semicolon at the end of Compound
Statement.

Example for Compound Statement

{
int a=10,b=20,c;
c = a + b;
%dDr.n”,c);
printf(“value of C is C: by Deepika Bhatia 31
}
Selection Statements :
Selection Statements are used in decisions making
situations we will look about selections statements in Later
Tutorials. Here is the few examples of Selection statements
•if
•if…else
•Switch

Iterative Statements :
These are also Called as Loops. If we want to Execute a part
of program many times we will use loops.We will going to explain
each and Every loop in Detail in Later Tutorials. Here is the List of
Basic loops in C language.
•for loop.
•while loop.
•do-while loop.
C by Dr. Deepika Bhatia 32
Jump Statements in C

These are Unconditional statements Jump statements are


useful for Transfer the Control one part of program to other part
of Program there are few Jump Statements in C
•goto.
•continue.
•break.
•return.

C by Dr. Deepika Bhatia 33


END

C by Dr. Deepika Bhatia 34

You might also like