Operators
Operators are the symbols that can be used to perform mathematical operations such as addtion, substration, relational, bitwise, conditional etc..
Categories of Operators
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Increment/Decrement Operators
- Conditional (Ternary) Operator:
Arithmetic Operators
This are used to perform mathematical operations on numerical values, such as integers and floating-point numbers
Operator |
Description |
example |
+ |
Addition |
a+b |
- |
Subtraction |
a-b |
* |
Multiplication |
a*b |
/ |
Division |
a/b |
% |
Modulus (Remainder) |
a%b |
Relational Operator
Relational operators in C are used to compare two values and determine the relationship between them
Operator |
Description |
example |
< |
a is less than b |
a
|
<= |
a is less than or equal b |
a
<=b |
> |
a is greater than b |
a>b |
>= |
a is greater than or equal |
a>=b |
== |
equal-to operator |
a==b |
!= |
not-equal operator |
a!=b |
Here we only testing two values if right menas prints 1 else 0.
Example
#include <stdio.h>
int main() {
int C = 10;
int Java = 5;
//use equal operator if this conditon true means print output as a 1 else 0
printf("%d", C==Java);
//same this also but operator only chage if true means print output as a 1 else 0
printf("%d", C>Java);
printf("%d", C>=Java);
printf("%d", C<=Java);
return 0;
}
Output
Not equal 0
qual 1
equal 1
Not equal 0
Useing if..else statement in this concept will discusess in detail in upcoming page
Example
#include <stdio.h>
int main()
{
int C = 10;
int Java = 5;
printf("Condition checking\n");
// Equal to (==) operator
if (C == Java) {
printf("%d i equal to %d\n", C, Java);
}
else if (C!=Java)
{
printf("%d is not equal to %d\n", C,
Java);
}
else {
printf("%d not matching given condition %d\n", C, Java);
}
return 0;
}
Output
Condition checking
10 is not equal to 5
Increment & Decrement Operator
Increment and decrement operators in C are used to increase or decrease the value of 1 to the operand
Operator |
Description |
example |
++ |
add 1 number |
++a |
-- |
decreas 1 number |
--a |
Example
#include <stdio.h>
int main()
{
int age ,age1;
age = 10;
// Here increase the 1 number using Increment operator.
age1=++age;
printf("The original age is %d\n" ,age);
printf("After increase the 1 to original
age : %d\n", age1);
return 0;
}
Output
The original age is 11
After increase the 1 to original age : 11