What are operators in C?
Operators are symbols that help in performing operations of mathematical and logical nature. The classification of C operators are as follows:
- Arithmetic
- Relational
- Logical
- Bitwise
- Assignment
- Conditional
- Special
Explanation of Operators in C
Below are the detailed explanation of operators in C:
1. Arithmetic Operators
These operators are responsible for performing arithmetic or mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), the remainder of the division (%), increment (++), decrement (–).
There are two types of arithmetic operators:
- Unary Operators: This type of operator works with a single value (operand) like ++ and –.
- Binary Operators: This type of operator works with two operands like +,-,*,/
Here is a tabular form of the number of arithmetic operators in C with the functions they perform.
Operator | Function |
+ | Adds two values |
– | Subtract a second value from first |
* | Multiply two values |
/ | Divide numerator by the denominator |
% | Remainder of division |
++ | Increment operator – increases integer value by one. |
— | Decrement operator – decreases integer value by one |
Example: C Program using arithmetic operators
#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;
}
Output :
2. Relational Operators
When we want to compare the values of two operands, then relational operators are used. If we want to check that is one operand is equal to or greater than other operands, then we use >= operator.
The below table lists of the relational operators in C with their functions.
Operator | Function | Example |
== | This will check if two operands are equal | 6 == 2 returns 0 |
!= | This will check if two operands are not equal. | 6 != 2 returns 1 |
> | This will check if the operand on the left is greater than operand on the right | 6 > 2 returns 1 |
< | This will check if the operand on the left is smaller than the right operand | 6 < 2 returns 0 |
>= | This will check if the left operand is greater than or equal to the right operand | 6 >= 2 returns 1 |
<= | This will check if operand on left is smaller than or equal to the right operand | 6 <= 2 return 0 |
Example: C Program using relational operators
#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;
}
Output:
3. Logical Operators
Logical Operators are used for True or False results.
The table below lists out the logical operators used in C
Operator | Function | Example (if a=1 and b=0) |
&& | Logical AND | (a && b) is false |
|| | Logical OR | (a || b) is true |
! | Logical NOT | (!a) is false |
Example: C Program using logical operators.
#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;
}
Output :
4. Bitwise Operators
These operators are used for bit-level operations on the operands. The operators are converted first to bit-level, and then calculations are performed.
Operator | Function |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
~ | Bitwise complement |
<< | Shift left |
>> | Shift right |
Example: C program for Bitwise AND
#include <stdio.h>
int main()
{
int a = 10, b = 8;
printf("Output = %d", a&b);
return 0;
}
Output:
Explanation:
10 = 00001010 (In Binary)
8 = 00001000 (In Binary)
Bit Operation of 10 and 8
00001010 & 00001000 = 00001000 = 8 (In decimal)
5. Assignment Operators
These types of operators are used to assign a value to a variable.
Operator | Function | Example |
= | This will assign values from right side operands to left side operand | a=b |
+= | This will add the right operand to the left operand and assign the result to left | a+=b is same as a=a+b |
-= | This will subtract the right operand from the left operand and assign the result to the left operand | a-=b is same as a=a-b |
*= | This will multiply the left operand with the right operand and assign the result to the left operand | a*=b is same as a=a*b |
/= | This will divide the left operand with the right operand and assign the result to the left operand | a/=b is same as a=a/b |
%= | This will calculate modulus using two operands and assign the result to the left operand | a%=b is the same as a=a%b |
6. Conditional Operators
Also known as Ternary Operator or? : Operator. These are used for decision-making.
Syntax:
Expression 1? Expression 2: Expression 3
Here,? Represents the IF condition.
7. Special Operators
Here are some special operators used in C
Operator | Function |
& | This operator is used to get the address of the variable.
Example: &a will give an address of a. |
* | This operator is used as a pointer to a variable.
Example: * a where * is a pointer to the variable a. |
size of () | This operator gives the size of the variable.
Example: The size of (char) will give us 1. |
Example: C program using a special operator
#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;
}
Output:
Recommended Articles
This has been a guide to C Operators. Here we have discuss operators used in C language with their syntax and examples. You may also look at the following articles to learn more.
- Comparison of C# vs JavaScript
- List of C-Command
- Career in C Programming
- Bitwise Operators in JavaScript
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses