In this detailed guide, we will discuss the operators in C programming and their types.
Operators and Their Types In C Programming
Operators are the symbols, used to carry out specific mathematical tasks. They are used to manipulate the data and variables. The following are the different types of operators in C programming:
- Arithmetic Operators
- Unary Operators
- Assignment Operators
- Logical or Boolean Operators
- Relational Operators
- Conditional Operators
- Bitwise Operators
1: Arithmetic Operators
These are the operators used to perform basic mathematical functions like Addition, Subtraction, or Multiplication. You can use these operators on almost all built-in data types of C programming. The following are the Arithmetic operators used in C programming:
Operators | Functions |
+ | Add 2 operands |
– | Subtract 2 operands |
* | Multiply 2 operands |
/ | Divide 2 operands |
% | The modulus operator gives the remainder of the division |
Example
In the below example, we have performed the above-mentioned arithmetic operations on the variable X and variable Y. The variable X holds the value 20 and Y holds the value 5:
int main()
{
int X = 20;
int Y = 5;
int result;
result = X+Y;
printf("Addition of X and Y is: %d \n",result);
result = X-Y;
printf("Subtraction of X and Y is: %d \n",result);
result = X*Y;
printf("Multiplication of X and Y is: %d \n",result);
result = X/Y;
printf("Division of X and Y is: %d \n",result);
result = X%Y;
printf("Modulus Division of X and Y is: %d \n",result);
return 0;
}
2: Unary Operators
There are two unique unary operators that are only supported by C language, increment ++ and decrement — operators. The increment operator adds 1 to the operand, and the decrement operator subtracts 1 from the operand.
The increment operator is written as:
The decrement operator is:
If we use the increment and decrement operator as a prefix it first adds or subtracts the variable value and then the result is assigned to the variable at left. If the operators are added before, it first returns the original value and then the operand is added or subtracted by 1.
Example
Below we have assigned values to two variables a and b and applied increment and decrement operators on them:
3: Assignment Operator
An assignment operator (=) is used for assigning the value to the variable in the program. Below are the assignment operators mentioned:
Operators | Function |
= | Assign the values to the operand |
+= | Add the value of the operand present on the right to the left operand |
-= | Subtract the value of the right operand from the left operand |
*= | Multiply the value of the right operand to the left operand |
/= | Divide the value of the right operand to the left operand |
%= | Take the modulus of two values and assign the value to the left operand |
Example
We have demonstrated the working of the assignment operators on the two operands X and Y in the below example:
int main()
{
int X = 10;
int result;
result = X;
printf("Value of result = %d\n", result);
result += X;
printf("Value of result = %d\n", result);
result -= X;
printf("Value of result = %d\n", result);
result *= X;
printf("Value of result = %d\n", result);
result /= X;
printf("Value of result = %d\n", result);
return 0;
}
4: Relational Operators
The relational operators are used in C programming to check the relationship between two variables. It can be used to compare the prices of the items or the age of two people The following are the relational operators used in C programming:
Operators | Functions |
== | Equal to |
> | Greater than |
< | Less than |
>= | Greater than equal to |
<= | Less than equal to |
!= | Not equal to |
Example
The below example is showing the working of the relational operators in C programming:
int main()
{
int a = 9;
int b= 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d <= %d is %d \n", a, b, a <= b);
return 0;
}
5: Logical Operators
There are four logical operators that are supported by C language:
Operators | Function |
Logical AND (&&) | True only if all conditions satisfy |
Logical OR (||) | If only one condition satisfies the result is true |
Logical NOT(!) | If Operand is 0 the result is true |
Bitwise NOT (~). | Inverts all the bits of the operand |
Example
The below example code explains the working of the logical operators in C:
int main()
{
int X = 10 , Y = 4, Z = 10, result;
result = (X == Y) && (Z > Y);
printf("(X == Y) && (Z > Y) is %d \n", result);
result = (X == Y) && (Z < Y);
printf("(X == Y) && (Z < Y) is %d \n", result);
result = (X == Y) || (Z < Y);
printf("(X == Y) || (Z < Y) is %d \n", result);
result = (X != Y) || (Z < Y);
printf("(X != Y) || (Z < Y) is %d \n", result);
result = !(X != Y);
printf("!(X != Y) is %d \n", result);
result = !(X == Y);
printf("!(X == Y) is %d \n", result);
result = !(X > Y);
printf("!(X > Y) is %d \n", result);
return 0;
}
6: Conditional Operators
The conditional operator in C is also known as the ternary operator because it takes three operands – the condition, statement 1, and statement 2. It evaluates the condition and returns either statement 1 or statement 2, depending on the result of a given condition that can be either true or false
- Condition: A Boolean expression that checks to be either true or false.
- Statement 1: An expression that is evaluated if the condition is true.
- Statement 2: An expression that is evaluated if the condition is false.
Example
In the below example, I have assigned the value to the number and then applied the condition, if the condition is true then statement 1 will be the output and if the condition is false then statement two will be the output:
7: Bitwise Operators
Bitwise operators in C manipulate data at the bit level, meaning they operate on individual bits within data types like integers. They cannot be applied to the double and float and are used for testing the bits and shifting them right or left.
Bitwise operators in C programming are given in the table below:
Operators | Function |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise Exclusive OR |
<< | Shift left |
>> | Shift Right |
~ | One’s complement |
Example
The following example shows a C program that uses bitwise operators:
int main() {
int a = 13; // binary 1101
int b = 7; // binary 0111
int result;
// Bitwise AND
result = a & b; // 1101 & 0111 = 0101 (decimal 5)
printf("a & b = %u\n", result);
// Bitwise OR
result = a | b; // 1101 | 0111 = 1111 (decimal 15)
printf("a | b = %u\n", result);
// Bitwise XOR
result = a ^ b; // 1101 ^ 0111 = 1010 (decimal 10)
printf("a ^ b = %u\n", result);
// Bitwise left shift
result = a << 2; // 1101 << 2 = 110100 (decimal 52)
printf("a << 2 = %u\n", result);
// Bitwise right shift
result = a >> 2; // 1101 >> 2 = 0011 (decimal 3)
printf("a >> 2 = %u\n", result);
// Bitwise NOT
result = ~a; // ~1101 = 0010 (decimal 2's complement representation of -14)
printf("~a = %d\n", result);
return 0;
}
Note: Bitwise operators are used to perform tasks at the bit level, meaning they operate on individual bits within a binary number. Boolean operators, on the other hand, are used to perform operations on logical values. They operate on Boolean values (true/false or 1/0) and are commonly used in decision-making processes or conditional statements.
Bottom Line
An operator is a symbol that instructs the compiler to carry out certain functions. The C language has several built-in operators including arithmetic, unary, assignment, logical, relational, conditional, Boolean, and bitwise. We have discussed them in detail and demonstrated them with the example output. Read the above section of the guide to get detailed information about these operators.