Operators
are symbols that are used to perform various operations on data, such as variables and constants. Operators are a fundamental part of C and are used to manipulate and process data in different ways. C provides a wide range of operators for performing tasks like arithmetic operations, comparison, logical operations, assignment, and more.
Z
=
X
/
Y
;
Here '/' is a Arithmetic Operator which divide the value of 'X' to 'Y' and it stored to 'Z'.
Types of Operators in C:
Here are some common categories of operators in C:
1. Arithmetic Operator
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
Operator Type
|
Operator(s)
|
Description
|
Arithmetic Operators
|
+, -, *, /, %
|
Perform basic arithmetic operations.
|
Relational Operators
|
==, !=, >, <, >=, <=
|
Compare values for equality and magnitude.
|
Logical Operators
|
&&, ||, !
|
Perform logical operations (AND, OR, NOT).
|
Bitwise Operators
|
&, |, ^, ~, <<, >>
|
Perform bitwise operations on integer values.
|
Assignment Operators
|
=, +=, -=, *=, /=, %=
|
Assign and combine values.
|
Increment/Decrement
|
++ (pre/post), -- (pre/post)
|
Increase or decrease variable values.
|
Conditional Operator
|
?:
|
A ternary operator for conditional expressions.
|
1. Arithmetic Operator:
These data types are essential for defining the type of data a variable can store in C and for performing various operations in your C programs.
#include
<stdio.h>
Copy Code
int
main
()
{
int
a
=
9
,
b
=
4
,
c
;
c
=
a
+
b
;
printf
(
"a+b = %d
"
,
c
);
c
=
a
-
b
;
printf
(
"a-b = %d
"
,
c
);
c
=
a
*
b
;
printf
(
"a*b = %d
"
,
c
);
c
=
a
/
b
;
printf
(
"a/b = %d
"
,
c
);
c
=
a
%
b
;
printf
(
"Remainder when A divided by B = %d
"
,
c
);
return
0
;
}
Output:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when A divided by B = 1
2. Relational Operator:
Relational operators compare values. They check if one value equals, is greater than, or is less than another. Examples include "==," which checks equality, ">=" for greater than or equal to, and "<=" for less than or equal to.
#include
<stdio.h>
Copy Code
int
main
()
{
int
a
=
10
;
int
b
=
20
;
printf
(
"a==b is %d
"
,
a==b
);
printf
(
"a!=b is %d
"
,
a!=b
);
printf
(
"a>b is %d
"
,
a>b
);
printf
(
"a<b is %d
"
,
a<b
);
printf
(
"a>=;b is %d
"
,
a>=b
);
printf
(
"a<=b is %d
"
,
a<=b
);
return
0
;
}
Output:
a == b is 0
a != b is 1
a > b is 0
a < b is 1
a >= b is 0
a <= b is 1
3. Logical Operator:
Relational operators compare values. They check if one value equals, is greater than, or is less than another. Examples include "==," which checks equality, ">=" for greater than or equal to, and "<=" for less than or equal to.
#include
<stdio.h>
Copy Code
int
main
()
{
int
a
=
10
;
int
b
=
20
;
printf
(
"a==b is %d
"
,
a==b
);
printf
(
"a!=b is %d
"
,
a!=b
);
printf
(
"a>b is %d
"
,
a>b
);
printf
(
"a<b is %d
"
,
a<b
);
printf
(
"a>=;b is %d
"
,
a>=b
);
printf
(
"a<=b is %d
"
,
a<=b
);
return
0
;
}
Output:
a == b is 0
a != b is 1
a > b is 0
a < b is 1
a >= b is 0
a <= b is 1
4. Bitwise Operator:
Bitwise operators are specialized tools in programming that allow you to work directly with the individual bits (0s and 1s) of binary representations of numbers. They take the binary representations of numbers and perform operations like AND, OR, XOR, and shifts on the bits.
These operators are useful when you need to perform operations at the lowest level of data representation, which can be faster and more memory-efficient than traditional mathematical operations.
#include
<stdio.h>
Copy Code
int
main
()
{
int
a
=
5
;
int
b
=
3
;
int
result_and
=
a & b
;
printf
(
"a & b = %d
"
,
result_and
);
int
result_or
=
a | b
;
printf
(
"a | b = %d
"
,
result_or
);
int
result_XOR
=
a ^ b
;
printf
(
"a ^ b = %d
"
,
result_XOR
);
return
0
;
}
Output:
a & b = 1
a | b = 7
a ^ b = 6
5. Assignment Operator:
Assignment operators are a fundamental element in programming. They are utilized to store a specific value in a variable. The process is straightforward: the left side of the assignment operator is the variable that will receive the value, and the right side is the value to be stored. It's vital to ensure that the data type of the value on the right side matches the data type of the variable on the left side; otherwise, the code won't compile and will produce an error. This ensures that you're assigning compatible values to your variables, maintaining data consistency in your program.
#include
<stdio.h>
Copy Code
int
main
()
{
int
a
=
5
;
int
b
=
3
;
int
result
;
result
=
a
;
printf
(
"a = %d
"
,
result
);
result
+=
b
;
printf
(
"a += b is %d
"
,
result
);
result
-=
b
;
printf
(
"a -= b is %d
"
,
result
);
result
*=
b
;
printf
(
"a *= b is %d
"
,
result
);
result
/=
b
;
printf
(
"a /= b is %d
"
,
result
);
return
0
;
}
Output:
a = 5
a += b is 8
a -= b is 5
a *= b is 15
a /= b is 5