C Operators
C Operators
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
Binary Operator Operates on two operands, For example: '" (used for division).
Type of
Description
Operator
Arithmetic
Arithmetic operators are used to perform mathematical operations.
operator
Relational
Relational operators are used to perform comparison operations.
operator
Assignment Assignment operator is used to assign value of the second operand to first.
operator
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
*
20
Multiplication 1* Operator produces result after multiplication
10 returns
Operator of operands.
200
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);
Operator by 1
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
variableN = expression.
Assignment operator are'+=', '- / = ' , '3='. The behaviour ofthese operators
is explained below.
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
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
= 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
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
Logical operator
Example
Operator Name Description
!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