0% found this document useful (0 votes)
0 views12 pages

C Operators

The document provides an overview of operators in the C programming language, categorizing them into arithmetic, assignment, relational, logical, and bitwise operators. It explains the functionality of unary, binary, ternary, and n-ary operators, along with specific examples of arithmetic and assignment operations. Additionally, it details increment and decrement operators, as well as relational and logical operators used for comparisons and boolean operations.

Uploaded by

INDIAN TECHING
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
0% found this document useful (0 votes)
0 views12 pages

C Operators

The document provides an overview of operators in the C programming language, categorizing them into arithmetic, assignment, relational, logical, and bitwise operators. It explains the functionality of unary, binary, ternary, and n-ary operators, along with specific examples of arithmetic and assignment operations. Additionally, it details increment and decrement operators, as well as relational and logical operators used for comparisons and boolean operations.

Uploaded by

INDIAN TECHING
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/ 12

COperators -Arithmetic, Assignment, Relational and Logical

Operator Basics

Operatorsin C language are symbols which tell the compiler to perform some
mathematical calculations or logical operations (we will look at in a while) on the
variables and constants. For example, consider the mathematical operation "10 +
2 a". Here '+' and '-' are called operators and the values on which operators work

(10, '2, and 'a') are known as operands. C programming language has wide
variety of operators which are categorized into different groups based on the type
of operations they perform.

Arity Description

Unary Operator Operates on single operand.

Binary Operator Operates on two operands, For example: '" (used for division).

Ternary Operator Operates on three operands. Example : conditional operator

n-ary Operator Operators that operate on more than 3 operands.


TYPES OF OPERATORS

Type of
Description
Operator

Arithmetic
Arithmetic operators are used to perform mathematical operations.
operator

Relational
Relational operators are used to perform comparison operations.
operator

Logical operators are used to perform basic operation of boolean algebra


Logical operator
(AND', 'OR, and NOT)

Bitwise operators are used to perform bit wise manipulation on operands.


Bitwise operator

Assignment Assignment operator is used to assign value of the second operand to first.
operator

Conditional Conditional operators operates on a condition and return result based on


operator the truth value of condition

Arithmetic Operators
Arithmetic operators are used to perform basicmathematical operationslike addition,
subtraction,multiplication and division. Therearefive arithmeticoperators availablein C(+,
/,6). All arithmetic operators are binary operators.ie;they operate on two operands to
produce the result. The table belowlists the arithmetic operators
Operator Name Description Example
Addition +'Operator adds two values. This operator 20+
Operator works on integer, float and character variables. 10 return 20

Operator produces a result after subtracting


Subtraction two values. It also works on integer, float,
20-
Operator 10 returns 10
character variables

*
20
Multiplication 1* Operator produces result after multiplication
10 returns
Operator of operands.
200

Operator produces result of division of first 20/


Division Operator
operand by second. 10 returns 2

Modulus %' Operator generates the remainder after 25 %

Operator integer division. 10 returns 5

Examples of Arithmetic Operators


Examples of Arithmetic Operators

1. #include <stdio.h>
2
3. int main()
4.{
5. int a = 25, b = 10;
6. printf("Sum: %din", a + b);
7. printf("Difference: %din", a - b);

8. printf("Product: %din", a*b);


9. printf("Quotient: %din", a /b);
10 printf("Remainder: %din", a % b);
11 return 0
12
13.
14. Output:
15. Sum: 35
16. Difference: 15
17. Product: 250
18 Quotient:2
19 Remainder: 5
Increment and Decrement operators (++, -)
There are two special arithmetic operators called increment and decrement operators available
are denoted as
C. They unary operators unlike all other arithmetic operators. The operators
are
: '++ (increment operator), '- (decrement operator).

Operator Name Description Example

Increments the value of operand by a++ This is equivalent to a =a


Increment Operator +1

Decrement Decrements the value of operand a--: This is equivalent to a =a-

Operator by 1

Pre Increment, Post


Variants of Increment and Decrement Operators
Increment, Pre Decrement and Post Decrement
known as pre increment,
There two different variants of increment and decrement operators
are

post increment, pre decrement and post decrement operators.


increment
'++ is used prefix of the operand, it's called pre
Pre Increment Operators: When
as
increment the value of operand using it in the before
operator. Pre increment operator will
expression.
'++' is used as postfix of the operand, it's called post
Post Increment Operators: When will increment the value of operand after using it in
increment operator. Post increment operator
will use non-incremented value of operand.
the expression. ie; The current expression decrement
When '- is used as prefix of the operand, it's called pre
Pre Decrement Operators: will decrement the value of operand before using it in the
operator. Pre decrement operator
expressIon.
When '" is used as postfix of the operand, it's called post
Post Decrement Operators: will decrement the value of operand after using it
decrement decrement
operator. Post operator
use non-decrement value of operand.
in the expression. ie; The current expression will

increment, pre decrement and post decrement operators are


Examples of pre increment, post
given below
Examples of pre increment, post increment, pre decrement and post decrement
operators are given below

1. #include <stdio.h>
2.
3. int main(void) {
4. int a = 5;
5. Pre increment operator. Value of 'a' will be incremented first and then
6. l used in the expression. So b is assigned value after a gets incremented.
7. int b =++a;
8. printf("a = %d, b = %din", a, b};

9.
10. a = 5;
11.// Post increment operator. The value of 'a' will be incremented only after
12.// it gets used in the expression. That means, first b will get assigned the
13.// value of 'a' and then value of 'a' will be incremented.
14. b = at+;
15. printf("a = %d, b = %din", a, b);
16.
17.a 55;
18.// Pre decrement operator. Similar to pre increment operator, pre
decrement
it in the
19./ operator decrements the value of operand before using
expression.
20. b-a;
21. printf(""a = %d, b =%din", a, b);
22.
23.a 5;
24.II Post decrement operator. Similarto post increment operator, post
decrement
it in the
25.// operator decrements the value of operand after using
expression.
26. b a - ;
27. printf("a = %d, b %din", a, b);
28. return 0;
29.
30.
31.
32.Output:
33.a 6, b =6
34.a = 6, b = 5

35. a =
4, b =4
36.a = 4, b = 5
Assignment Operator
to left side variable. The
Assignment operator assigns value of the expression on the right side
base assignment operator is '=. In C, you can use this operator like the following
variable =

expression

C also allows multiple a s s i g n m e n t s t a t e m e n t like v a r i a b l e l = variable2

variableN = expression.

Assignment operator are'+=', '- / = ' , '3='. The behaviour ofthese operators
is explained below.

Operator Name Description Example

This operator perform addition of operand on left a += 10: This is


Add and Assignment side with operand right side and the result will be equivalent to a = a
+
Operator assigned to operand at left side. +10
This operator subtracts the operand on right side -10:Thisis
Subtract and from the operand on left side and the result will be equivalent to a
Assignment Operator 10
assigned to left side operand.

This operator multiplies the operand on right side a 10: This is


Multiplication and with the operand on left side and the result will be equivalent to a = a
Assignment Operator 10
assigned to left side operand.

This operator divides the operand on left side with a /= 10: This is
Division and the operand on right side and the result will be equivalent to a = a

Assignment Operator assigned to left side operand. 10

This operator computes the modulus of left side a 10: This is


Modulus and equivalent to a = a

operand by right side operand and the result will be


Assignment Operator assigned to left side operand. 10

Examples of Assignment Operator


Examples of Assignment Operator

1. #include <stdio.h>
2.
3. int main()
4. {
5. int a 25, b = 10;
6. (a + b;
7. printf("a' aftera += bi %din", a);
8
9. a 25, b 10;
10. a- b;
11. printf("a' after a = b: %d\n", a);
12.
13. a 25, b 10;
14. a * b;
15. printf("a' after a *= b: %din", a);

16.
17. a = 25, b = 10;

18. a/ b;
19. printf("a' after al=b: %dln", a);
20
21. a = 25, b = 10;

22. a %= b;
23. printf("a' after a 1%= b: %d\n", a);
24
25. return 0; a tb
26.)
27.
28.Outpuut:
29.'a' after a += b: 35
30.'a after a -= b: 15
31.'a' after a "= b: 250
32.'a' after a = b: 2
33.a' after a %= b: 5

a -
Relational operator
to relate or
and are used
two operands)
Relational operators are binary operators(operates on >=). It the
four relational operators in C (i.e <, <=, >,
compare fwo operands. There are otherwise.
will return 1 and returns 0
relationship between the operands is correct, it
and =) as well for
Chas two equality operator (==
Apart from four relational operators,
comparing operands.

Example
Operator Name Description

less than second


10< 5 returns 0
Less than Checks if the first operand is and 5 < 10 returns 1
and returns 1 if it's true, else returns 0
operator operand

= 10 returns 1
'Less than or Checks if the first operand is less than or equals 10
and 10 <= 5 returns
equals to' to second operand and returns 1 if it's true, else

operator returns 0

Checks if the first operand is greater


than 10 5 returns 1
Greater than' else
second operand and returns 1 if it's true, and 5 > 10 returns 0
operator returns 00

10 >= 10 returns 1
Greater than or Checks if the first operand is greater than or
to second operand and returns 1 if it's and 5>= 10 returns
equals to' equals
true, else returns 0 0
operator

10 10 returns 1
Checks if the two operands are equal are
and 10 == 1 returns
Equality operator returns 1 if it's true, else returns 0

Checks if the two operands are equal are 10!= 10 returns 0


Non-equality
returns 1 if they are not equal, else returns 0
operator and 10 != 1 returns
Examples of Relational Operator

Logical operator
Example
Operator Name Description

it a && b returns true if


returns true if both the operands
'AND' operator both a and b are non
'Logical else return
evaluates to true (non-zero),
on
operates
&.& AND zero
Operator false

returns true if any of the operands it


a breturns true if
OR' operator either a or b are non-
else return
Logical OR on evaluates to true (non-zero),
operates zero
Operator false

!a returns true if a is
unary operator (it operates
only
'NOT operator is a
false. Else returns
Logical true if the operand is false
on one operand). It returns
NOT is true false
Operator and returns false if the operand
Examples of Logical Operators

You might also like