[go: up one dir, main page]

100% found this document useful (1 vote)
15 views15 pages

Unit 3 - Operators and Expression - 1704342525014

The document provides an overview of various operators in the C programming language, categorizing them into arithmetic, assignment, unary, comparison, shift, bit-wise, logical, and miscellaneous operators. It explains the functionality and examples of each operator type, including operator precedence and associativity rules. Additionally, it illustrates how these operators work with examples and outputs.

Uploaded by

Abinash Pokhrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
15 views15 pages

Unit 3 - Operators and Expression - 1704342525014

The document provides an overview of various operators in the C programming language, categorizing them into arithmetic, assignment, unary, comparison, shift, bit-wise, logical, and miscellaneous operators. It explains the functionality and examples of each operator type, including operator precedence and associativity rules. Additionally, it illustrates how these operators work with examples and outputs.

Uploaded by

Abinash Pokhrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Operators:

Operators are symbols or combinations of symbols that directs the computer to


perform some operation upon operands. C includes a large number of operators,
which fall into several different categories.

● Arithmetic Operators
● Assignment Operators
● Unary Operators
● Comparison (Relational) Operators
● Shift Operators
● Bit-wise Operators
● Logical Operators
● Miscellaneous Operators

Arithmetic Operators

There are five arithmetic Operators in C. They are:

Suppose variable a holds 10 and b holds 20


Operator Description Expression Value
+ Adds two a+b 30
operands
- Subtracts second a-b -10
operand from the
first
* Multiplies both a*b 200
operands
/ Divides numerator b/a 2
by de-numerator

Prepared By: Deependra Karki


% Modulus Operator b%a 0
(remainder after
an integer
division)

Suppose X1 and X2 are character-type variables that represent the character M


and U respectively. Some arithmetic expressions that make use of these variables
are shown below.

X1 + X2 =162
X1+ X2+ ‘5’ = 215

Note that M is encoded as 77 , U is encoded as 85, and 5 is encoded as 53 in ASCII


character set.

Assignment Operator:

Assignment operator assigns the value of the right operator or expression to the
variable on the left side. There are many variations of assignment operator as
described below;

Operator Description Example

= Assigns values from right side operands to C = A + B will assign


left side operands. value of A + B into C

Prepared By: Deependra Karki


+= Adds the operands and assigns the result C += A is equivalent
to the left operand. to C = C + A

-= Subtracts right operand from the left


C -= A is equivalent to
operand and assigns the result to left
C=C-A
operand.

*= Multiplies right operand with the left


C *= A is equivalent
operand and assigns the result to left
to C = C * A
operand.

/= Divides left operand with the right


C /= A is equivalent to
operand and assigns the result to left
C=C/A
operand.

%= Takes modulus using two operands and C %= A is equivalent


assigns the result to the left operand. to C = C % A

Unary Operators:

A unary operator in C, is an operator that takes a single operand in an expression


or a statement. There are basically two unary operators in C. They are:

Prepared By: Deependra Karki


Operator Description Example Explanation
++ Increases the value of a++ Equivalent to a=a+1
the operand by one.
-- Decreases the value of a-- Equivalent to a=a-1
the operand by one.

The increment operator ++, can be used in two ways:

● As a prefix
In prefix, the operator precedes the variable i.e. ++var . In this form the
value of the variable is first incremented and then used in the expression as
illustrated below;

var1=20; var2=++var1;

This code is equivalent to the following set of codes:

var1=20; var1=var1+1; var2=var1;

At the end, both variables var1 and var2 store value 21.

● As a postfix

Likewise, in postfix, the operator follows the variable i.e. var++. In this form,
the value of variable is used in the expression and then incremented as
illustrated below;

var1=20; var2=var1++;

The equivalent of this code is:

var1=20; var2=var1; var1=var1+1;

Prepared By: Deependra Karki


At the end, var1 has the value 21 while var2 is set to 20.

Example: Post decrement and Pre decrement

Output:

54

43

Prepared By: Deependra Karki


Example: Post Increment and Pre Increment

Output:

56

67

Comparison Operator

Comparison operators evaluate to true or false. They are also called relational
operators.

Assume variable A holds 10 and variable B holds 20, then

Operator Description Example

== Checks if the values of two operands are (A == B) is not true.


equal or not, if yes then condition
becomes true.

Prepared By: Deependra Karki


!= Checks if the values of two operands are (A != B) is true.
equal or not, if values are not equal then
the condition becomes true.

> Checks if the value of left operand is (A > B) is not true.


greater than the value of right operand,
if yes then condition becomes true.

< Checks if the value of left operand is less (A < B) is true.


than the value of right operand, if yes
then condition becomes true.

>= Checks if the value of left operand is (A >= B) is not true.


greater than or equal to the value of
right operand, if yes then condition
becomes true.

<= Checks if the value of left operand is less (A <= B) is true.


than or equal to the value of right
operand, if yes then condition becomes
true.

Shift Operators

Data is stored internally in binary format. A bit can have a value of one or zero.
Eight bits form a byte. Shift operators work on individual bits in a byte. Using the

Prepared By: Deependra Karki


shift operator involves moving the bit pattern left or right. We can use them only
on integer data type and not on the char , float , or double data types.

Operator Description Example Explanation


>> Shifts bits to the a=10>>3 The result of this
right, filling sign is 10 divided by
bir at the left. 2^3.
<< Shifts bits to the a=10<<3 The result if this is
left, filling zeros at 10 multiplied by
the right. 2^3.

If the int data type occupies four byte in the memory, the rightmost eight bits of
the number are represented in binary as “00001010”

When we do right shift by 3, the result is “00000001” which is equivalent to 1 and


when we do left shift by 3, the result is “01010000” which is equivalent to 80.

Bit-wise operators

Bitwise operator works on bits and performs bit-by-bit operation. The truth
tables for &, |, and ^ are as follows −

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

Prepared By: Deependra Karki


1 1 1 1 0

1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows −

A = 0011 1100

B = 0000 1101

-----------------------------

Binary And Operator:

A&B = 0000 1100

Binary Or Operator:

A|B = 0011 1101

Binary XOR Operator:

A^B = 0011 0001

Binary Ones Complement Operator:

~A = 1100 0011

Logical Operators

There are following logical operators supported by the C language.

Assume variable A holds 1 and variable B holds 0, then

Prepared By: Deependra Karki


Operator Description Example

&& Called Logical AND operator. If both the (A && B) is false.


operands are non-zero, then the condition
becomes true.

|| Called Logical OR Operator. If any of the two (A || B) is true.


operands is non-zero, then the condition
becomes true.

! Called Logical NOT Operator. Use to reverse !(A && B) is true.


the logical state of its operand. If a
condition is true, then the Logical NOT
operator will make false.

Miscellaneous Operators

The following table lists some other operators that C supports.

Sr.No Operator & Description

1 sizeof

sizeof operator returns the size of a variable. For example, sizeof(a),


where ‘a’ is integer, and will return 4.

Prepared By: Deependra Karki


2 Condition ? X : Y

Conditional operator (?). If Condition is true then it returns value of


X otherwise returns value of Y.

Example:

result: (marks>50 ? “pass”: “fail”) ;

In the above example, if the marks obtained is more than 50, the
result will be passed else fail.

3 ,

Comma operator causes a sequence of operations to be performed.


The value of the entire comma expression is the value of the last
expression of the comma-separated list.

4 . (dot) and -> (arrow)

Member operators are used to reference individual members of


classes, structures, and unions.

5 Cast

Casting operators convert one data type to another. For example,


int(2.2000) would return 2.

Prepared By: Deependra Karki


6 &

Pointer operator & returns the address of a variable. For example


&a; will give the actual address of the variable.

7 *

Pointer operator * is pointer to a variable. For example *var; will


pointer to a variable var.

Precedence of Operators

If more than one operator is involved in an expression, C language has a predefined rule
of priority for the operators. This rule of priority of operators is called operator
precedence.

In C, precedence of arithmetic operators( *, %, /, +, -) is higher than relational


operators(==, !=, >, <, >=, <=) and precedence of relational operator is higher than logical
operators(&&, || and !).

Here, operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom. Within an expression, higher precedence operators will
be evaluated first.

Prepared By: Deependra Karki


Prepared By: Deependra Karki
Example:

Output:

Value of (a + b) * c / d is : 90

Value of ((a + b) * c) / d is : 90

Value of (a + b) * (c / d) is : 90

Value of a + (b * c) / d is : 50

Prepared By: Deependra Karki


Associativity of Operators

If two operators of same precedence (priority) are present in an expression,


Associativity of operators indicates the order in which they execute.

Prepared By: Deependra Karki

You might also like