0% found this document useful (0 votes)
9 views13 pages

Operators

The document provides an overview of operators in C programming, classifying them into unary, binary, logical, relational, and bitwise operators, along with examples of their usage. It also discusses special operators like the comma operator, sizeof(), and the ternary operator, as well as increment and decrement operators. Additionally, it covers operator precedence, associativity, and type conversion in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Operators

The document provides an overview of operators in C programming, classifying them into unary, binary, logical, relational, and bitwise operators, along with examples of their usage. It also discusses special operators like the comma operator, sizeof(), and the ternary operator, as well as increment and decrement operators. Additionally, it covers operator precedence, associativity, and type conversion in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming in C

 Operators in C
 An operator is a symbol that tells the computer to perform certain mathematical or logical
calculations.
 Operator act as connectors.
 Operators specify the type of operation to be carried out.
 In other words, we can say that an operator operates the operands.
 The values that can be operated by this operators are called operands

 Classification of operators

1. Unary Operator
 The operator that acts on a single or only one operand is called unary operator.
 The C unary Operators are
a) unary minus
b) Logical NOT
c) Bitwise Complementation
a) unary minus
 Any positive operand associated with unary minus operator gets its value
changed to negative.
 Example: a=3 b=4
c= a+(-b)
=3+(-4)
=3-4
=-1
2. Binary Operator
 The operator that acts on two operands is called binary operator.
 Hence the name binary operators.
 The binary operators are
a) Arithmetic operators

UNIT: III [C-Operators & Expressions] Page 1


Programming in C

b) Logical operators
c) Relational operators
d) Bitwise operators
a) Arithmetic operators
 These are used to perform the basic arithmetic operation such as addition,
subtraction, multiplication, division, and modulus.
 A=5 B=3
NOTE: Modulus operator cannot be used with floating point number
Operations Operators Precedence Associativity Example
Addition + 2 L to R A+B=5+3=8
Subtraction - 2 L to R A-B=5-3=2
Multiplication * 1 L to R A*B=5*3=15
Division / 1 L to R A/B=5/3=1
Modulus % 1 L to R A%B=5%3=2

// Working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}
b) Logical operators
 Logical operators are mostly used for decision making.
 A logical operator returns either 0 or 1 whether the condition is true or false.

UNIT: III [C-Operators & Expressions] Page 2


Programming in C

Operators Precedence Associativity Meaning


&& 2 L to R Logical AND
(doubleampersand)

|| 3 L to R Logical OR
( double pipeline)
! 1 L to R Logical NOT
(exclamation)

 Logical AND
 This operator is used to perform ANDing operation on two logical operands.
 It performs multiplication operation
 The ‘&&’ operator returns true when both the conditions under consideration are
satisfied.
 Otherwise, it returns false.

Truth Table
Operand 1 Operand 2 Operand 1 &&operand 2
False False False
False True False
True False False
True True True
 Logical OR
 This operator is used to perform ORing operation on two logical operands.
 it performs addition operation.
 The ‘||’ operator returns true even if one (or both) of the conditions under consideration
is satisfied. Otherwise, it returns false.

Truth Table

Operand 1 Operand 2 Operand 1 || operand 2


False False False
False True True
True False True
True True True

 Logical NOT
 This operator is used to obtain the logical complement or inverse of the given operand.
 The ‘!’ operator returns true the condition in consideration is not satisfied.
 Otherwise, it returns false
UNIT: III [C-Operators & Expressions] Page 3
Programming in C
Truth Table Operand !Operand
False True
True False
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}
c) Relational operators
 They are used to compare two operands in an expression they result in either a true or
false value
/ / Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);

UNIT: III [C-Operators & Expressions] Page 4


Programming in C
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}
If A=5 B=3

d) Bitwise operators
Operators Meaning Precedence Associativity Example
 All data items stored in the computer memory as a sequence of bits.
< Less than 1 L to R A<B :FALSE
<= Less than or 1 L to R A<=B:FALSE
equal
> Greater than 1 L to R A>B:TRUE
>= Greater than 1 L to R A>=B:TRUE
or equal
== Equals to 2 L to R A==B :FALSE
!= Not equals 2 L to R A!=B:TRUE
to
 C provides 6 bit wise operation these operators work with int and char type.
 floating point cannot be used

Operators Symbol name Meaning


& Ampersand Bitwise AND
| Pipeline Bitwise OR
^ Caset Bitwise XOR
~ Tilde 1’s Complement
<< Double less than Left shifting of bits
>> Double greater than Right shifting of bits
Truth Table: AND(&)

OR(|)
a b a&b(Result)
a b a|b(Result)
0 0 0
0 0 0
0 1 0
0 1 1
1 0 0
1 0 1
1 1 1
1 1 1

XOR

UNIT: III [C-Operators & Expressions] Page 5


Programming in C

a b a^b(Result)
0 0 0
0 1 1
1 0 1
1 1 0

Example

a=4 b= 3 Find (a&b), (a|b) , (a^b)

Bitwise complement operator:

 This is used to obtain the one’s complement of a binary sequence.


 That is each 0 in a binary sequence is changed to 1 and each 1 in a binary sequence is changed to
0

Example: a=10

1010

=0101 -> 1’s complement of a

Bitwise Left shifting

 The left shift and right shift operators change the contents of an input number.
 syntax:
<variable><shift operator><nob>;
where,
variable : an int or char type data element
shift operator : left shift or right shift operator
nob : number of bits to be shifted

Example: a>>1 : right shift by 1

UNIT: III [C-Operators & Expressions] Page 6


Programming in C
a<<2 : left shift by 2

 Special Operators Of C
1. Comma operator
 The comma operator is basically associated with the for statement.
 It is also used to link two or more related expression together.
example: sum=(a=12,b=22,a+b);
e
2. sizeof() operator
 It returns the size (i.e number of bytes)of an operand.
 It is a function and therefore has to be written in lowercase letters.
 sizeof(operand);

Example: x=sizeof (int);

y= sizeof(sum);

3. Ternary Operator/Conditional
 This is used to test the relationship between two variables
 This operator takes 3 operands
UNIT: III [C-Operators & Expressions] Page 7
Programming in C

 ? Symbol is used as conditional operator


Syntax:< expression > ? < value 1 > : < value 2 > ;
where:

expression : relational expression

value 1 : value to be assigned when the result of expression is true

value 2 : value to be assigned when the result of expression is false

Example: c=a<b ? a : b;

Hence, c will be assigned the value of a if a is less than b otherwise, it will be assigned the value of b

 Increment and decrement Operator


 Increment
 Increment Operators are the unary operators used to increment or add 1 to the operand value.
 The Increment operand is denoted by the double plus (++)symbol
 The symbol can be placed either before or after an integer variable
 It has two types, Pre Increment and Post Increment Operators.

 Pre-increment Operator

 The pre-increment operator is used to increase the original value of the operand by 1 before
assigning it to the expression.

Syntax

a = ++b;
This is equivalent to two assignment statement
b=b+1;and
a=b;

 Post increment Operator

 The post-increment operator is used to increment the original value of the operand by 1 after
assigning it to the expression.

Syntax

a = b++;
UNIT: III [C-Operators & Expressions] Page 8
Programming in C

This is equivalent to two assignment statement


b=b+1;and
a=b;

Example: int a=3;

a++ --- a=a+1

=3+1;

++ a --- a=a+1

=3+1;

 Decrement Operator
 Decrement Operator is the unary operator, which is used to decrease the original value of the
operand by 1.
 The decrement operator is represented as the double minus symbol (--).
 It has two types, Pre Decrement and Post Decrement operators.

 Pre-decrement Operator

 The pre-decrement operator is used to decrease the original value of the operand by 1 before
assigning it to the expression.

Syntax

a = --b;
This is equivalent to two assignment statement
b=b-1;and
a=b;

 Post-decrement Operator

 Post decrement operator is used to decrease the original value of the operand by 1 after assigning
to the expression.

 a = b--;
 This is equivalent to two assignment statement
 b=b-1;and
 a=b;

UNIT: III [C-Operators & Expressions] Page 9


Programming in C
 Precedence ,Associatively of all C Operators

#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );

UNIT: III [C-Operators & Expressions] Page 10


Programming in C
return 0;
}

 Evaluation of Arithmetic Expression

 Arithmetic expressions are equivalent from left to right.

 Expressions involving high priority operators are evaluated first.

 For example:

1. x*y/z

 There are two operators (* and /) having the same priority.

 In this case, the expression is scanned from left to right.

 The expression x*y is evaluated first. then this result is divided by the value z.

2. a+b*c/d-e

3. (number%10)+(number/10)

4. 2*((i/3)+4*(j-2) given i=8 j=5

Rules:

 If the given expression involves parentheses, the expression inside the parentheses must be
evaluated first.

 If a unary minus is present in the expressions, then the term associated with a unary minus
must be evaluated before the other expressions.

UNIT: III [C-Operators & Expressions] Page 11


Programming in C
 Type conversion

 Typecasting is converting one data type into another one.

 It is also called as data conversion or type conversion in C language.

Syntax:

(data type)variable

Two types of type casting operations:

1. Implicit type casting


2. Explicit type casting

Implicit type casting Implicit type casting means conversion of data types without losing its original
meaning. This type of typecasting is essential when you want to change data types without changing the
significance of the values stored inside the variable.

#include<stdio.h>
int main()
{
short a=10; //initializing variable of short data type
int b; //declaring int variable
b=a; //implicit type casting
printf("%d\n",a);
printf("%d\n",b);
}
Output:

10
10

Explicit type casting

In implicit type conversion, the data type is converted automatically. There are some scenarios in
which we may have to force type conversion.

#include<stdio.h>
int main()
{
float a = 1.2;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of a is %f\n", a);
printf("Value of b is %d\n",b);
return 0;
}

Output:

sum = 120.000000

UNIT: III [C-Operators & Expressions] Page 12


Programming in C

UNIT: III [C-Operators & Expressions] Page 13

You might also like