You are on page 1of 29

Operators

Prepared By:
Asst. Prof. Jyotsnarani Tripathy
Introduction
• C language supports a rich set of built-in
operators. An operator is a symbol that tells
the compiler to perform a certain
mathematical or logical manipulation.
• Operators are used in programs to manipulate
data and variables.
Types
• C operators can be classified into following types:
• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise operators
• Assignment operators
• Conditional operators
• Special operators
Arithmetic operators
• C supports all the basic arithmetic operators. The
following table shows all the basic arithmetic
operators.
Operator Description

+ adds two operands

- subtract second operands from first

* multiply two operand

/ divide numerator by denominator

% remainder of division
Relational operators
• The following shows all relation operators supported by C.

• Operator Description
• == Check if two operand are equal
• != Check if two operand are not equal.
• > Check if operand on the left is greater than operand on the
right
• < Check operand on the left is smaller than right operand
• >= check left operand is greater than or equal to right operand
• <= Check if operand on left is smaller than or equal to right
operand
Logical operators
• C language supports following 3 logical
operators. Suppose a = 1 and b = 0,
Operator Description Example

&& Logical AND (a && b) is false

|| Logical OR (a || b) is true

! Logical NOT (!a) is false


Bitwise operators
• Bitwise operators perform manipulations of
data at bit level. These operators also perform
shifting of bits from right to left. Bitwise
operators are not applied to float or
double(These are datatypes, we will learn
about them in the next tutorial).
Cont..

Operator Description

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

<< left shift

>> right shift


Cont..
a b a&b a|b a^b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

The bitwise shift operator, shifts the bit


value. The left operand specifies the value
to be shifted and the right operand
specifies the number of positions that the
bits in the value have to be shifted. Both
operands have the same precedence.
Cont..
• In order to clearly understand bitwise operators, let us see
the truth table for various bitwise operations and
understand how it is associated with boolean algebra.
• AND – Both the operands should have boolean value 1 for
the result to be 1.
• OR – At least one operand should have boolean value 1 for
the result to be 1.
• XOR (EXCLUSIVE OR) – Either the first operand should have
boolean value 1 or the second operand should have
boolean value 1. Both cannot have the boolean value 1
simultaneously.
Cont..
• Consider two operands, a and b with values:
a = 26 and b=14
Therefore, a & b is computed as follows:
Find the binary equivalent of a and b:
• Perform boolean AND/OR/EXCLUSIVE OR operation bit by
btw
• Convert the answer into its corresponding decimal form.
• Bitwise AND
• a = 26 = 1 1 0 1 0
• b = 14 = 0 1 1 1 0
• ________
• a & b = 0 1 0 1 0 which is equal to 10
Cont..
Bitwise OR
• a = 26 = 1 1 0 1 0
• b = 14 = 0 1 1 1 0
• ________
• a | b = 1 1 1 1 0 which is equal to 30
Bitwise XOR
• a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 0 1 0 0 which is equal to 20
Bitwise One’s Complement
• a = 26 = 1 1 0 1 0
• Reversing its bits, we get 0 0 1 0 1 which is equal to 5 but this is not the correct
answer! The correct answer is: -(a+1) which is -27 which is in accordance with
two’s complement.
Example
• a = 0001000
• b=2
• a << b = 0100000
• a >> b = 0000010
Assignment Operators
• = assigns values from right side operands to left side
operand a=b
• += adds right operand to the left operand and assign the
result to left a+=b is same as a=a+b
• -= subtracts right operand from the left operand and assign
the result to left operand a-=b is same as a=a-b
• *= mutiply left operand with the right operand and assign the
result to left operand a*=b is same as a=a*b
• /= divides left operand with the right operand and assign the
result to left operand a/=b is same as a=a/b
• %= calculate modulus using two operands and assign the
result to left operand a%=b is same as a=a%b
Conditional operator

• The conditional operators in C language are known by two more


names

• Ternary Operator
• ? : Operator
• It is actually the if condition that we use in C language decision
making, but using conditional operator, we turn the if condition
statement into a short and simple operator.

• The syntax of a conditional operator is :

• expression 1 ? expression 2: expression 3


Explanation:
• The question mark "?" in the syntax represents the if
part.
• The first expression (expression 1) generally returns
either true or false, based on which it is decided
whether (expression 2) will be executed or (expression
3)
• If (expression 1) returns true then the expression on
the left side of " : " i.e (expression 2) is executed.
• If (expression 1) returns false then the expression on
the right side of " : " i.e (expression 3) is executed.
Special operator
• The Comma Operator
• Type cast Operator
• Reference operator or Address Operater ("&")
• Dereference operator ("*") or Pointer
Operater
• Double Pointer operator ("**")
• sizeof operator
Comma Operator

• The Comma Operator


• The Comma operator can be used to link the
related expressions together.
• Example For Comma Operator
• Comma Operator In for Loops
• for(i=0,j=1;i>10:i++,j++)
• Comma Operator In while Loops
• While(c<10,c--)
Type cast Operator
• Syntax:
• ( type )
• Explanation
• converts a to the specified type.
• Note that the use of a parenthesized type in a method
declaration or definition is not an example of the use of the type
cast operator.
• Example For Type Cast Operator
• float s= 12.5; int a;
• a = (int) s;
• now a value is 12.
Reference operator or Address Operater
("&")
• The reference operator noted by ampersand ("&"), is also
a unary operator in c languages that uses for assign
address of the variables. It returns the pointer address of
the variable. This is called "referencing" operater.

• Syntax

• data_type x;
• data_type *pt;
• pt = &x
Dereference operator ("*") or Pointer
Operater
• The dereference operator or indirection
operator, noted by asterisk ("*"), is also a unary
operator in c languages that uses for pointer
variables. It operates on a pointer variable, and
returns l-value equivalent to the value at the
pointer address. This is called "dereferencing"
the pointer.

• data_type *pt;
Double Pointer operator ("**")
• Double Pointer is, that double pointer points
to another pointer variable address.

• data_type **pt;
sizeof operator
• sizeof returns the size of a variable or datatype

• sizeof (data_type)
Increment and decrement operators in C

• The Increment and Decrement Operators in C


are some of the Operators, which are used to
increase or decrease the value by 1. For
instance, Incremental operator ++ is used to
increase the existing variable value by 1 (x = x
+ 1). And decrement operator – – is used to
decrease or subtract the existing value by 1 (x
= x – 1).
Prefix and Postfix in C
• If you observe the above syntax, we can assign the C increment and decrement
operators either before operand or after the operand. When ++ or — is used before
operand like: ++x, –x then we call it as prefix, if ++ or — is used after the operand
like: x++ or x– then we called it as postfix.

• Let’s explore the prefix and postfix of increment and decrement operators in C

• ++i (Pre-increment): It will increment the value of i even before assigning it to the
variable i.
• i++ (Post-increment): The operator will return the variable value first (i.e, i value)
then only i value is incremented by 1.
• –i (Pre decrement): It will decrement the value of i even before assigning it to the
variable i.
• i– (Post decrement): The operator returns the variable value first (i.e., i value), then
only i value decremented by 1.
• /* Increment and Decrement Operators in C as Prefix and Postfix */

• #include<stdio.h>
int main()
• {
int x = 10,y = 20, a = 5, b= 4;
printf("---- PRE INCREMENT OPERATOR EXAMPLE---- \n");
• printf("Value of x : %d \n", x); //Original Value
• printf("Value of x : %d \n", ++x); // using Pre increment Operator
• printf("Value of x Incremented : %d \n", x); //Incremented value
printf("----POST INCREMENT OPERATOR EXAMPLE---- \n");
• printf("Value of y : %d \n", y); //Original Value
• printf("Value of y : %d \n", y++); // using Post increment Operator
• printf("Value of Incremented y : %d \n", y); //Incremented value

• printf("----PRE DECREMENT OPERATOR EXAMPLE---- \n");


• printf("Value of a : %d \n", a); //Original Value
• printf("Value of a : %d \n", --a); // using Pre decrement Operator
• printf("Value of decremented y : %d \n", a); //decremented value

• printf("----POST DECREMENT OPERATOR EXAMPLE---- \n");


• printf("Value of b : %d \n", b); //Original Value
• printf("Value of b : %d \n", b--); // using Post decrement Operator
• printf("Value of decremented b : %d \n", b); //decremented value
return 0;
• }
examples
• #include <stdio.h>
• int main()
• {
• int a = 20; /* 20 = 010100 */
• int b = 21; /* 21 = 010101 */
• int c = 0;

• c = a & b; /* 20 = 010100 */
• printf("AND - Value of c is %d\n", c );

• c = a | b; /* 21 = 010101 */
• printf("OR - Value of c is %d\n", c );

• c = a ^ b; /* 1 = 0001 */
• printf("Exclusive-OR - Value of c is %d\n", c );

• }
• Output:

• AND - Value of c is 20
• OR - Value of c is 21
• Exclusive-OR - Value of c is 1
Examples
• #include <stdio.h>
• int main() {
• int a = 20; /* 20 = 010100 */
• int c = 0;

• c = a << 2; /* 80 = 101000 */
• printf("Left shift - Value of c is %d\n", c );

• c = a >> 2; /*05 = 000101 */


• printf("Right shift - Value of c is %d\n", c );
• return 0;
• }
• Output:

• Left shift - Value of c is 80


• Right shift - Value of c is 5
examples
• # include <stdio.h>

• void main()
• {
• int a, b, c, big ;

• printf("Enter three numbers : ") ;

• scanf("%d %d %d", &a, &b, &c) ;

• big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;

• printf("\nThe biggest number is : %d", big) ;
• }

You might also like