The C language supports a wide set of built-in operators. An operator is a symbol that tells the compiler to perform a particular mathematical or logical operation. Operators are used programmatically to manipulate data and variables. Operators are symbols that help to perform operations of a mathematical and logical nature.
Types of Operators:
1. Arithmetic Operators:
These operators are arithmetic or mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), remainder of division (%), increment (++), decrement (-). ..
Types of Arithmetic Operators:
- Unary Operators: This type of operator works with a single value (operand) such as ++ or -.
- Binary Operators: this type of operator works with two operands, such as +, -, *, /.
This is a picture of the number of C arithmetic operators and the functions they perform.
For example:
Here code:
#include <stdio.h>
int main()
{
int a = 12, b = 6, c;
c = a + b;
printf("a+b = %d \n", c);
c = a - b;
printf("a-b = %d \n", c);
c = a *b;
printf("a*b = %d \n", c);
c = a / b;
printf("a/b = %d \n", c);
c = a % b;
printf("Remainder when a divided by b = %d \n", c);
return 0;
}
2. Relational Operators:
Relational operators are used when comparing the values of two operands. Use the> = operator to ensure that one operand is greater than or equal to the other.
The following image is a list of C relational operators and their functions.
For example:
Here Code:
#include <stdio.h>
int main()
{
int a = 7, b = 7, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}
3. Logical operators:
Logical operators are used for true or false results.
The following image shows the logical operators used in C.
For example:
Here Code:
#include <stdio.h>
int main()
{
int a = 8, b = 8, c = 12, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
4. Bitwise Operators:
These operators are used for bit-level operations on operands. The operator is first converted to the bit level and then the calculation is performed.
For example:
Here Code:
#include <stdio.h>
int main()
{
int a = 10, b = 8;
printf("Output = %d", a&b);
return 0;
}
5. Assignment Operators:
These types of operators are used to assign values to variables.
6. Conditional Operators:
Also, the ternary operator or? :operator. These are used for decision making.
Syntax: Expression 1? Expression 2: Expression 3
7. Special Operators:
Some special operators used in C are:
For example:
Here Code:
#include <stdio.h>
int main()
{
int *ptr, q;
q = 40;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
Conclusion:
Operators are the basic foundation of the C / C ++ programming language. You can now use other conditions to perform any mathematical, logical, or relational operation.
References:
- https://www.programiz.com/c-programming/c-operators
- https://www.tutorialspoint.com/cprogramming/c_operators.htm
- https://www.studytonight.com/c/operators-in-c.php
- https://www.w3schools.in/c-tutorial/operators/
- https://fresh2refresh.com/c-programming/c-operators-expressions/c-logical-operators/
- https://data-flair.training/blogs/operators-in-c-and-cpp/
- https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B