Computer Science Unit-2 Sem 1
Computer Science Unit-2 Sem 1
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
Binary operators: An operator that acts upon two operands. It has following types-
BINARY OPERATORS
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 OR A= 1010
B= 1100
-------------
A|B= 1110
2
Ternary operator: An operator that acts upon three operands.
{
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;
V Op = Exp;
It is equivalent to V = V Op Exp;
Increment operator ++
Decrement operator --
Both the operators are used in two ways:
Prefix operator : ++ variable or - - variable
Postfix operator : variable ++ or variable - -
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
Ans: Precedence: There are many types of operators. Each operator has a priority, that is known as
precedence of operators.
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.
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