0% found this document useful (0 votes)
27 views

Computer Science Unit-2 Sem 1

The document discusses different types of operators in C language including unary, binary, ternary and assignment operators. It explains each operator type with examples and also covers operator precedence and associativity rules.

Uploaded by

aditya67857
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Computer Science Unit-2 Sem 1

The document discusses different types of operators in C language including unary, binary, ternary and assignment operators. It explains each operator type with examples and also covers operator precedence and associativity rules.

Uploaded by

aditya67857
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit-2

Ques 1: What is an Operator? Explain all the types of operators with example used in C.

Ans: Operators: An operator is a special symbol which is used to perform some specific operation.

Types of operators:
C OPERATORS

UNARY OPERATORS BINARY OPERATORS TERNARY OPERATORS

Unary operators: An operator that acts upon one operand.

Example: Unary minus, Increment operator, Decrement operator etc.

Binary operators: An operator that acts upon two operands. It has following types-

BINARY OPERATORS

ARITHMETIC LOGICAL RELATIONAL BITWISE


OPERATORS OPERATORS OPERATORS OPERATORS

Binary Operator Types of operator Example

void main( )
Addition (+) {
Subtraction (-) int a=5, b=3, c;
Arithmetic Multiplication (*) c = a % b;
Division (/) printf(“%d”, c);
Remainder (%) }

void main( )
{
Less than (<) int a=5, b=3;
Less than or equal to (<=) if(a > b)
Relational Greater than (>) printf(“a is greater”);
Greater than or equal to (>=) else
Equal to (==) printf(“b is greater”);
Not equal to (!=) }

1
void main( )
{
int a=5, b=3, c=7;
Logical AND (&&) if(a>=b && a<=7)
Logical Logical OR (||) printf(“a is between 5 and 7”);
Logical NOT (!) else
printf(“a is not between 5
and7”);
}

Bitwise AND A= 1010


B= 1100
-------------
A&B= 1000

Bitwise OR A= 1010
B= 1100
-------------
A|B= 1110

Bitwise XOR A= 1010


Bitwise AND (&) B= 1100
Bitwise OR (|) -------------
Bitwise Bitwise XOR (^) A^B= 0110
Bitwise compliment (~)
Bitwise left shift (<<) Bitwise compliment
Bitwise right shift (>>) A= 1010
------------
~A= 0101

Bitwise left shift


A= 0101
---------------
A<<1= 1010

Bitwise right shift


A= 0101
--------------
A>>1= 0010

2
Ternary operator: An operator that acts upon three operands.

Example: Conditional operator

Conditional operator (? :) : The syntax of conditional operator is as follows-

Expression1 ? Expression2 : Expression3

Example: void main( )

{
int a, b, c;
printf(“Enter the value of a and b”);
scanf(“%d %d”, &a, &b);
c = (a>b) ? a : b;
printf(“\nGreater number is= %d”, c);
}

Assignment operators: Assignment operators are used to assign the result of an expression to a
variable. It has a simple form

Variable = Expression;

In addition, C has a set of ‘shorthand’ assignment operators of the form

V Op = Exp;

It is equivalent to V = V Op Exp;

Where, V is variable, Op is Operator, and Exp is Expression.

Example: (1) a+=1 => a=a+1


(2) a-=1 => a=a-1
(3) a * = (n + 1) => a = a * (n + 1)
(4) a/=2 => a=a/2
(5) a%=3 => a=a%3

Increment and decrement operators: C provides two very useful operators.

 Increment operator ++
 Decrement operator --
Both the operators are used in two ways:
 Prefix operator : ++ variable or - - variable
 Postfix operator : variable ++ or variable - -

The operator ++ add 1 to the operand, while - - subtracts 1.

++a or a++ => a=a+1


- -a or a- - => a=a-1

3
Example: void main()

{
int a, b, x;
a = 5;
x = ++a;
printf(“%d”, x);
b = 5;
x = b++;
printf(“%d”, x);
}

Output: 6 5

Ques 2: Define operator precedence and associativity.

Ans: Precedence: There are many types of operators. Each operator has a priority, that is known as
precedence of operators.

Precedence or Hierarchy of Operators:


Operators Type
() Parenthesis
++, --, !, ~ etc Unary operators
*, /, % Arithmetic and modulus
+, - Arithmetic
= Assignment

Associativity:
If there is an expression in which more than one operator of same precedence are available, than
the expression is evaluated by associativity rules.

1. Left to Right associativity


2. Right to Left associativity

Example:
r=a+b-c*d;
if (a = 1, b = 2, c = 3, d = 4)
r=a+b–3*4
 r = a + b – 12
 r = 1 + 2 – 12
 r = 3 – 12
r=–9

You might also like