Operators - Like Tools for Your Code.
In other words we can say that which type of data will store in a variable.
Imagine you're building something with your toys. You use different tools for different tasks, right? In C language, operators are a bit like those tools.
They help you do things with your data - add, subtract, compare, and more.
Diagram of Common Operators:
+--------------------------+
| Operators |
+--------------------------+
| Arithmetic Operators|
+ - * / %
+--------------------------+
| Relational Operators|
== != < > <= >=
+--------------------------+
| Logical Operators |
&& || !
+--------------------------+
| Assignment Operator |
=
+--------------------------+
| Increment/Decrement |
++ --
+--------------------------+
| Bitwise Operators |
& | ^ ~ << >>
+--------------------------+
Arithmetic Operators:
These are like math tools.
+ (addition): Adds two numbers.
- (subtraction): Subtracts the right operand from the left.
* (multiplication): Multiplies two numbers.
/ (division): Divides the left operand by the right.
% (modulus): Gives the remainder after division
int result = 10 + 5; // result is now 15
Relational Operators:
These help you compare things.
== (equal to): Checks if two values are equal.
!= (not equal to): Checks if two values are not equal.
< (less than): Checks if the left value is less than the right.
> (greater than): Checks if the left value is greater than the right.
<= (less than or equal to): Checks if the left value is less than or equal to the right.
>= (greater than or equal to): Checks if the left value is greater than or equal to the right.
int x = 10;
int y = 5;
if (x > y)
{
// This block will be executed because 10 is greater than 5
}
Logical Operators:
These are like logical tools.
&& (logical AND): Returns true if both conditions are true.
|| (logical OR): Returns true if at least one condition is true.
! (logical NOT): Returns true if the condition is false and vice versa.
int a = 1;
int b = 0;
if (a && !b)
{
// This block will be executed because a is true and b is not true
}
Assignment Operator:
This is like giving a value to something.
= (assignment): Assigns the value on the right to the variable on the left.
int age = 25; // The variable 'age' now holds the value 25
Increment/Decrement:
These are like counting tools.
++ (increment): Increases the value of a variable by 1.
-- (decrement): Decreases the value of a variable by 1.
int count = 5;
count++; // Now 'count' is 6
Bitwise Operators:
These are like tools for working with individual bits (0 or 1) in binary.
& (bitwise AND): Performs a bitwise AND operation.
| (bitwise OR): Performs a bitwise OR operation.
^ (bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.
~ (bitwise NOT): Flips the bits, changing 0 to 1 and vice versa.
<< (left shift): Shifts bits to the left.
>> (right shift): Shifts bits to the right.
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a & b; // result is 1 (binary: 0001)
These operators are like your trusty tools in the C programming toolbox. Each one has its job, and you use them to build and manipulate your code!
Below is a simple C program that uses various operators available in the C language:
Output is as follow:
0 Comments
If you have any doubts, Please let me know