0% found this document useful (0 votes)
18 views49 pages

C Programming Operators Explained

The document provides an overview of C programming operators, including classifications such as unary, binary, and ternary operators, as well as arithmetic, relational, logical, bitwise, and assignment operators. It explains the syntax and functionality of each operator type with examples and code snippets. Additionally, it covers increment and decrement operators, and the concept of bitwise operations and their implications in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views49 pages

C Programming Operators Explained

The document provides an overview of C programming operators, including classifications such as unary, binary, and ternary operators, as well as arithmetic, relational, logical, bitwise, and assignment operators. It explains the syntax and functionality of each operator type with examples and code snippets. Additionally, it covers increment and decrement operators, and the concept of bitwise operations and their implications in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Vidya Jyothi Institute of Technology

Two week FDP on C-


Programming
Organized by
Department of CSE, IT & AI
C Operators

by

Dr. [Link]
Professor,
Dept. of CSE
1/46
 Operators are symbols which take one or more

operands or expressions and perform arithmetic ,

logical etc. mathematical computations.

Ex: + - * /
 Operands are variables or expressions which

are used in conjunction with operators to evaluate

the expression.

Ex: a, b, c 2/46
Classifications of Operators:
1. Based on Number of operands
Unary Operators
Binary Operators
Ternary Operators

2. Based on the type of computation


performing
◦ Arithmetic Operators
◦ Relational Operators
◦ Logical Operators
◦ Bitwise Operators
◦ Assignment Operators
◦ Misc Operators
Unary Operators: These operators take
only one operand
Syntax: UnaryOperator Operand
 unary+ and unary-
They represent Positive and negative vars/values

ex: -10 +10 -x +k


 Increment and decrement Operators ++ --
They increase and decrease operand value
by 1
ex: ++a x++ --k b—

Example: int a=10; ++a; printf(“%d”, a);


Binary Operators: These operators
take Two operands
a+b  Addition
a-b  Subtraction
a*b  Multiplication
a/b  Division
a%b  Remainder or modulo
a>b  Comparision
a=100  Assignment

Ternary Operator: Operator takes
Three operands

There is only one ternary operator i.e. ? :


True

Y=(Condition)? Expr1:Expr2;

False

If Condition is true ? then Y value is Expr1 : otherwise Y value is Expr2

Ex: x=20; Y = (x > 0)? 100: 20;  Y=100


2. Based on the type of computation
performing,
operators are classified into:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators
 An arithmetic operator performs mathematical
operations such as addition, subtraction,
multiplication, division etc on numerical values
(constants and variables).
Operator Description Example
+ Adds two int A=10,
operands. B=20;
A + B = 30
− Subtracts second A − B = -10
operand from the
first.
* Multiplies both A * B = 200
operands.
/ Divides numerator B/A=2
by de-numerator.
% Modulus Operator B%A=0
and remainder of
after an integer
division.
++ Increment A++ = 11
operator
increases the
integer value by
one.
#include <stdio.h>
int main() Output:
{ int a = 9,b = 4, c;
c = a+b; a+b = 13
printf("a+b = %d \n",c); a-b = 5
c = a-b; a*b = 36
printf("a-b = %d \n",c); a/b = 2
c = a*b; Remainder of a
printf("a*b = %d \n",c); divided by b=1
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder of a divided by b = %d \
n",c);
return 0;
}
C Increment and Decrement
Operators
 C programming has two operators increment ++ and
decrement -- to change the value of an operand by
1.
 Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1.
 These two operators are unary operators, meaning
they only operate on a single operand.
#include <stdio.h>
int main() Output:
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
++a = 11
printf("++a = %d \n", ++a); --b = 99
printf("--b = %d \n", --b); ++c =
printf("++c = %f \n", ++c); 11.500000
printf("--d = %f \n", --d);
--d =
return 0;
99.50000
}

Here, the operators ++ and -- are used as prefixes.


These two operators can also be used as postfixes like a++ an
Relational Operators
 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.


Operato
Meaning of Operator Example
r

5 == 3 is
== Equal to
evaluated to 0

5 > 3 is evaluated
> Greater than
to 1

5 < 3 is evaluated
< Less than
to 0

5 != 3 is
!= Not equal to
evaluated to 1

Greater than or 5 >= 3 is


>=
equal to evaluated to 1

5 <= 3 is
<= Less than or equal to
evaluated to 0
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b,
a == b);
printf("%d == %d is %d \n", a, c, Output:
a == c);
5 == 5 is 1
printf("%d > %d is %d \n", a, b,
a > b); 5 == 10 is 0
printf("%d > %d is %d \n", a, c, a 5 > 5 is 0
> c);
5 > 10 is 0
printf("%d < %d is %d \n", a, b,
a < b); 5 < 5 is 0
printf("%d < %d is %d \n", a, c, a 5 < 10 is 1
< c); 5 != 5 is 0
printf("%d != %d is %d \n", a, b,
a != b);
5 != 10 is 1
printf("%d != %d is %d \n", a, c, 5 >= 5 is 1
a != c); 5 >= 10 is 0
printf("%d >= %d is %d \n", a, b, 5 <= 5 is 1
a >= b);
5 <= 10 is 1
Logical Operators
 An expression containing logical operator
returns either 0 or 1 depending upon
whether expression results true or false.
 Logical operators are commonly used
in decision making in C programming.
 Used to combine two or more
conditions/relations in C
Operato
Meaning Example
r

If c = 5 and d = 2 then,
Logical AND. True
expression
&& only if all operands
((c==5) && (d>5))
are true
evaluates to 0.

Logical OR. True If c = 5 and d = 2 then,


|| only if either one expression ((c==5) ||
operand is true (d>5)) evaluates to 1.

Logical NOT. True


If c = 5 then, expression !
! only if the operand
(c==5) evaluates to 0.
is 0
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", Output
result);
(a == b) && (c > b)
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", is 1
result);
result = (a == b) || (c < b); (a == b) && (c < b)
printf("(a == b) || (c < b) is %d \n", is 0
result);
result = (a != b) || (c < b); (a == b) || (c < b) is
printf("(a != b) || (c < b) is %d \n",
1
result);
result = !(a != b); (a != b) || (c < b) is 0
printf("!(a != b) is %d \n", result);
result = !(a == b); !(a != b) is 1
printf("!(a == b) is %d \n", result); !(a == b) is 0
return 0;
}
 (a == b) && (c > 5) evaluates to 1 because both
operands (a == b) and (c > b) is 1 (true).
 (a == b) && (c < b) evaluates to 0 because operand (c
< b) is 0 (false).
 (a == b) || (c < b) evaluates to 1 because (a == b) is 1
(true).
 (a != b) || (c < b) evaluates to 0 because both
operand (a != b) and (c < b) are 0 (false).
 !(a != b) evaluates to 1 because operand (a != b) is 0
(false). Hence, !(a != b) is 1 (true).
 !(a == b) evaluates to 0 because (a == b) is 1 (true).
Hence, !(a == b) is 0 (false).
Bitwise Operators
 During computation, mathematical

operations like: addition, subtraction,

multiplication, division, etc are converted to

bit-level which makes processing faster and

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

>> Binary Right Shift Operator. The


left operands value is moved right A =60;
by the number of bits specified by A >> 2 = 15 i.e., 0000 1111
the right operand.
Meaning of
Operators
operators

& Bitwise AND

| Bitwise OR

Bitwise exclusive
^
OR

Bitwise
~
complement

<< Shift left

>> Shift right


p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
 Assume A = 60 and B = 13 in binary
format,

A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
bitwise AND operation of two integers 12
and 25
 12 = 00001100 (In #include <stdio.h>
Binary) int main()
 25 = 00011001 (In
{
Binary)
int a = 12, b = 25;
 Bit Operation of 12
printf(“Result = %d",
and 25
a&b);
00001100
return 0;
& 00011001
}
-------------- Output:
Result = 8
00001000 = 8 (In
decimal)
Bitwise Operator 1
 The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.
 12 = 00001100 (In
Binary)
 25 = 00011001 (In
Binary) #include <stdio.h>
 Bitwise OR Operation of int main()
12 and 25 {
00001100 int a = 12, b = 25;
| 00011001 printf("Output = %d",
----------------- a|b);
00011101 = 29 (In return 0;
decimal) }

Output:
Output=29
Bitwise XOR (exclusive OR) operator
^

 Theresult of bitwise XOR operator is 1 if the


corresponding bits of two operands are
opposite. It is denoted by ^.
 12 = 00001100 (In #include <stdio.h>
Binary) int main()
 25 = 00011001 (In
{
Binary) Bitwise XOR
int a = 12, b = 25;
printf("Output = %d",
Operation of 12 and 25
a^b);
00001100 return 0;
^ 00011001 }
---------------

00010101 = 21 (In Output:


decimal)
Output=21
Bitwise complement operator ~

 Bitwise compliment operator is an unary


operator (works on only one operand). It changes
1 to 0 and 0 to 1. It is denoted by ~.

35 = 00100011 (In Binary)


Bitwise complement Operation of 35
~ 00100011
------------
11011100 = 220 (In decimal)
Twist in bitwise complement
operator in C Programming

 The bitwise complement of 35 (~35) is -36


instead of 220, but why?
 For any integer n, bitwise complement
of n will be -(n+1).
 To understand this, We should have the

knowledge of 2's complement.


2's Complement
 Two's complement is an operation on binary
numbers. The 2's complement of a number
is equal to the complement of that number
plus 1.
For example:
Decimal Binary 2’s Complement
0 000000 -(11111111+1) = -00000000 = -
00 0(decimal)
1 000000 -(11111110+1) = -11111111 = -
01 256(decimal)
12 000011 -(11110011+1) = -11110100 = -
00 244(decimal)
220 110111 -(00100011+1) = -00100100 = -
00 36(decimal)
Bitwise complement
 bitwise complement of N = ~N (represented in 2's
complement form) 2'complement of ~N= -(~(~N)+1) = -
(N+1)
#include <stdio.h>
int main()
{
printf("Output = %d\n",~35);
printf("Output = %d\n",~-12);
return 0;
}

 Output
Output = -36
Output = 11
Shift Operators in C programming

 Right Shift Operator


Right shift operator shifts all bits towards
right by certain number of specified bits. It is
denoted by >>.

212 = 11010100 (In binary)


212>>2 = 00110101 (In binary) [Right shift
by two bits]
 Left Shift Operator
Left shift operator shifts all bits towards left
by a certain number of specified bits. The bit
positions that have been vacated by the left
shift operator are filled with 0.
The symbol of the left shift operator is <<.

212 = 11010100 (In binary)


212<<1 = 110101000 (In binary) [Left shift
by one bit]
Shift Operators

#include <stdio.h>
int main()
{
int num=212, i; Right Shift by 0:
212
for (i=0; i<=2; ++i)
Right Shift by 1:
printf("Right shift by %d: %d\n", i, 106 Right Shift by
num>>i); 2: 53
Left Shift by 0:
printf("\n");
212 Left Shift by
for (i=0; i<=2; ++i) 1: 424 Left Shift
by 2: 848
printf("Left shift by %d: %d\n", i,
num<<i);
return 0;
}
Assignment Operators
 An assignment operator is used for assigning
a value to a variable.
 The most common assignment operator is =.
Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
Output
printf("c = %d\n", c); c=5
c -= a; // c is 5 c = 10
c=5
printf("c = %d\n", c);
c = 25
c *= a; // c is 25 c=5
printf("c = %d\n", c); c=0
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Misc Operators ↦ sizeof & ternary
 Besides the operators discussed above, there are a few

other important operators

Operator Description
including sizeof and ? : supported by the CExample
Language.
sizeof() sizeof(a); If a is
Returns the size of a
integer, will return
variable.
2.
& &a; returns the
Returns the address of a
actual address of
variable.
the variable.
* Value at operator OR *a;
indirection operator
If Condition is true ?
?: Conditional Expression. then value X :
otherwise value Y
(type) cast_expr int i=10;
Explicit Type conversion
#include <stdio.h>
main()
{
int a = 4;
short b;
double c;
int* ptr;
/* example of sizeof operator */
printf("Line 1 - Size of variable a = %d\n", sizeof(a) );
printf("Line 2 - Size of variable b = %d\n", sizeof(b) );
printf("Line 3 - Size of variable c= %d\n", sizeof(c) );
/* example of & and * operators */
ptr = &a; /* 'ptr' now contains the address of 'a'*/
printf("value of a is %d\n", a); Output:
printf("*ptr is %d.\n", *ptr);
Line 1 - Size of variable a = 4
/* example of ternary operator */ Line 2 - Size of variable b = 2
a = 10; Line 3 - Size of variable c= 8
b = (a == 1) ? 20: 30; value of a is 4
printf( "Value of b is %d\n", b ); *ptr is 4
b = (a == 10) ? 20: 30; Value of b is 30
printf( "Value of b is %d\n", b ); Value of b is 20
OPERATOR PRECEDENCE
 Consider the following arithmetic
operation:
- left to right
6 / 2 * 1 + 2 = 5
- right to left Inconsistent
6/2 * 1 + 2 = 1
- using parentheses
answers!
= (6 / 2) * (1 + 2)
= (3) * (3)
= 3 * 3
= 9

34/46
OPERATOR PRECEDENCE
 Operator precedence: a rule used to clarify
unambiguously which operations (operator
and operands) should be performed first in
the given (mathematical) expression.
 Use precedence levels that conform to the
order commonly used in mathematics.
 However, parentheses take the highest
precedence and operation performed from
the innermost to the outermost
parentheses.
35/46
OPERATOR PRECEDENCE
 Precedence and associativity of C
operators affect the grouping and
evaluation of operands in expressions.
 Is meaningful only if other operators
with higher or lower precedence are
present.
 Expressions with higher-precedence
operators are evaluated first.

36/46
OPERATOR PRECEDENCE
Precedence and Associativity of C Operators
Symbol Type of Operation Associativity
[ ] ( ) . –> postfix ++ and postfix –– Expression Left to right
prefix ++ and prefix –– sizeof()
Unary Right to left
& * + – ~ !
typecasts Unary Right to left
* / % Multiplicative Left to right
+ – Additive Left to right
<< >> Bitwise shift Left to right
< > <= >= Relational Left to right
== != Equality Left to right
& Bitwise-AND Left to right
^ Bitwise-exclusive-OR Left to right
| Bitwise-inclusive-OR Left to right
&& Logical-AND Left to right
|| Logical-OR Left to right
? : Conditional-expression Right to left
= *= /= %=
Simple and compound
+= –= <<= >>= &= Right to left
assignment
^= |=
, Sequential evaluation Left to right 37/46
OPERATOR PRECEDENCE
 The precedence and associativity (the order
in which the operands are evaluated) of C
operators.
 In the order of precedence from highest to
lowest.
 If several operators appear together, they
have equal precedence and are evaluated
according to their associativity.
 All simple and compound-assignment
operators have equal precedence.
38/46
OPERATOR PRECEDENCE
 Operators with equal precedence such
as + and -, evaluation proceeds
according to the associativity of the
operator, either from right to left or from
left to right.
 The direction of evaluation does not
affect the results of expressions that
include more than one multiplication
(*), addition (+), or binary-bitwise (& |
^) operator at the same level.
39/46
OPERATOR PRECEDENCE
 e.g:
3 + 5 + (3 + 2) = 13 – right to left
(3 + 5) + 3 + 2 = 13 – left to right
3 + (5 + 3) + 2 = 13 – from middle
 Order of operations is not defined by the
language.
 The compiler is free to evaluate such
expressions in any order, if the compiler can
guarantee a consistent result.

40/46
THANK YOU

You might also like