C-Operator

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

C-Operators

PRESENTED BY- LOCHAN RAJ DAHAL


C Operators
► An operator is simply a symbol that is used to perform operations. There can be many types of operations like
arithmetic, logical, bitwise, etc.
► There are following types of operators to perform different types of operations in C language.

► Arithmetic Operators
► Relational Operators
► Shift Operators
► Logical Operators
► Bitwise Operators
► Ternary or Conditional Operators
► Assignment Operator
► Misc Operator
Precedence of Operators in C
► The precedence of operator species that which operator will be evaluated first and next. The associativity
specifies the operator direction to be evaluated; it may be left to right or right to left.
► Let's understand the precedence by the example given below:

The value variable willint value=10+20*10;


contain 210 because * (multiplicative operator) is evaluated before + (additive operator).
► The data items in which any operation is carried out are called operands.


x +andy+ is an operator and calculating sum is an operation.
Here x and y are operands
► Operators which require two operands are called binary operators and which takes single operand are called
unary operators.
Precedence of Operators in C
► The precedence and associativity of C operators is given below:
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right


Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right


Arithmetic operators in C
► Operators used in the arithmetic operation like addition, subtraction, multiplication or division are called
arithmetic operators.

Arithmetic operator Meaning

+ Addition or unary plus

– Subtraction or unary minus

* Multiplication

/ Division

% Modulo division
Relational Operator in C
► Relational operators are used to compare two operators depending on their relation.
► For example, for comparing the price of two things. The value of a relational operator is either 1 or 0. If the
specified relation is true then 1 else 0.
a>b
► Here, > is a relational operator and it will return 1 if a is greater than b else it will return 0.
Relational operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Logical Operator in C
► Logical operators are used when more than one condition is tested. They are used to combine more than
one relational expressions.
► For example:
a > b && c == 1
► Here && is relational operator used to combine two relational expressions a > b and c == 1.
Logical operator Meaning
&& logical AND
|| logical OR
! logical NOT

Example: Logical AND (&&) operator


If we need to make sure that both conditions are true before performing a certain task. In this case, logical AND && is used:
if ( gender == 1 && age <= 10 )
++childNumber;
In this case, if condition is true only when both of the conditions gender == 1 and age <= 10 are true.
Finally, if the combined condition is true, then only childNumber is increased by 1.
Logical Operator in C
Example: Logical OR (||) operator
If we wish to ensure that either or both of two conditions are true then we use logical OR (||) operator.

if (mark < 40 || attendance < 15)


puts("Student is not qualified for exam");
In this case, if any of the conditions is true then if statement is also true and the message Student is not qualified for exam is
printed.
Assignment Operator in C
► The assignment operator is used to assign a value or a result to a data item or a variable.
► = is the assignment operator.
► For example:
a=5
► Here, = is assignment operator which assigns value 5 to variable a.
► Assume:
int c = 2, d = 3, e = 4, f = 6, g = 8

Assignment Operator Sample expression Explanation Assigns


+= c += 2 c=c+2 4 to c
-= d -= 1 d=d–1 1 to d
*= e *= 2 e=e*2 8 to e
/= f /= 3 f=f/3 2 to f
%= g %= 4 g=g%4 2 to g

Note: Don’t get confused between equality operator == and assignment operator =
Bitwise Operator in C
► Bitwise operators are used to manipulate data at a bit level. They are used for testing or shifting the bit.

Bitwise operator Meaning


& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
Conditional operator in C
► Conditional operator is a ternary operator that takes three operands.
► Syntax
x = exp1 ? a : b
► Here, if exp1 is non zero i.e. TRUE then the value of a will be assigned to x and if exp1 is zero i.e.
FALSE then the value of b is assigned to x.

Increment / Decrement operator in C


► C provides an increment operator ++ and decrement operator --. The functionality of ++ is to add 1 unit to
the operand and -- is to subtract 1 from the operand.
► For example
++ a;
-- b;
► Here ++a is equivalent to a = a + 1 and --b is equivalent to b = b - 1.
Increment / Decrement operator in C
• There are two kinds of increment and decrement operator i.e. prefix and postfix.

• If the operator is used before the variable i.e. ++a then it is called prefix increment operator.

• If the operator is used after variable i.e. a++ then it is called postfix increment operator.

• In the prefix operator, first 1 is added and then the value is assigned to the variable.
• In postfix operator, first the value is assigned then only 1 is added and the added value is assigned.
Operator Sample expression Explanation
x is increased by 1, then use the
++ ++x
value of x

Use the current value of x and


++ x++
then increment x by 1

x is decreased by 1, then use


–– – -x
the value of x

Use the current value of x and


–– x- –
then decrement x by 1
Working of post-increment and pre-increment operator
#include <stdio.h>
int main()
{
int i;
i = 4;
printf( "%d\n", i );
printf( "%d\n", i++ ); //post increment
printf( "%d\n\n", i );
i = 4; Output
printf( "%d\n", i );
4
printf( "%d\n", ++i ); //preincrement 4
printf( "%d\n", i ); 5
return 0;
4
}
5
5
Special operators in C
► Besides these fundamental operators, C provides some other special operators like comma operator and
size of operator

Comma operator in C
► It is used to combine more than one expression. A comma linked list of expressions are evaluated left to
right.

sizeof operator in C
• sizeof operator is an operator which when used with operand returns the number of bytes occupied by the
operand.

For example

x = sizeof( a );
Here, the size occupied by variable a will be assigned to x.

You might also like