Operators are special symbols that tell the C compiler to perform an operation on one or more values. Operators are used for calculations, comparisons, assignments, logical decisions, and many other programming tasks.
An operator is a symbol or combination of symbols that performs an operation on one or more operands.
int result = 10 + 20;
Here, + is an operator and 10 and 20
are operands.
An operand is a value, variable, or expression on which an operator performs an operation.
int sum = a + b;
Here, a and b are operands, while +
is the operator.
Important operator categories in C include:
sizeof and commaArithmetic operators are used to perform mathematical calculations.
| Operator | Meaning |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Remainder |
The + operator adds two operands.
int a = 10;
int b = 20;
int sum = a + b;
printf("%d", sum);
Output:
30
The - operator subtracts the second operand from the first.
int a = 30;
int b = 10;
int result = a - b;
printf("%d", result);
Output:
20
The * operator multiplies two operands.
int a = 10;
int b = 5;
int result = a * b;
printf("%d", result);
Output:
50
The / operator performs division. When both operands are integers,
integer division is performed.
int a = 20;
int b = 5;
int result = a / b;
printf("%d", result);
Output:
4
The % operator gives the remainder of integer division.
int a = 17;
int b = 5;
int remainder = a % b;
printf("%d", remainder);
Output:
2
Relational operators compare two values.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
The == operator checks whether two values are equal.
int a = 10;
int b = 10;
printf("%d", a == b);
The expression produces 1 when the values are equal and
0 when they are not.
The != operator checks whether two values are different.
int a = 10;
int b = 20;
printf("%d", a != b);
The result is 1 because the values are different.
int age = 20;
printf("%d", age > 18);
The expression evaluates to 1 because 20 is greater
than 18.
int age = 15;
printf("%d", age < 18);
The expression evaluates to 1 because 15 is less
than 18.
The = operator assigns the value of the expression on its right
to the object on its left.
int age;
age = 20;
The variable age receives the value 20.
C provides shorthand assignment operators.
| Operator | Example | Meaning |
|---|---|---|
+= |
a += 5; |
a = a + 5; |
-= |
a -= 5; |
a = a - 5; |
*= |
a *= 5; |
a = a * 5; |
/= |
a /= 5; |
a = a / 5; |
%= |
a %= 5; |
a = a % 5; |
The ++ operator increases an integer or compatible arithmetic
object by one.
int count = 5;
count++;
printf("%d", count);
Output:
6
The -- operator decreases an integer or compatible arithmetic
object by one.
int count = 5;
count--;
printf("%d", count);
Output:
4
The logical AND operator && produces true when both
operands are nonzero.
int age = 20;
int marks = 80;
printf("%d", age >= 18 && marks >= 40);
Both conditions are true, so the result is 1.
The logical OR operator || produces true when at least one
operand is nonzero.
int age = 16;
printf("%d", age < 18 || age == 20);
At least one condition is true, so the result is 1.
The logical NOT operator ! reverses the logical truth value of
its operand.
int value = 0;
printf("%d", !value);
Output:
1
Bitwise operators work on the individual bits of integer operands.
| Operator | Meaning |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left shift |
>> |
Right shift |
The conditional operator is a shorthand way to choose between two expressions based on a condition.
int age = 20;
char *result = (age >= 18) ? "Adult" : "Minor";
printf("%s", result);
If the condition is true, the first expression is selected; otherwise the second expression is selected.
The sizeof operator gives the size in bytes of a type or object.
int age;
printf("%zu", sizeof(age));
The result type of sizeof is size_t.
The unary & operator obtains the address of an object.
int age = 20;
printf("%p", (void *)&age);
The address can be stored in a suitable pointer variable.
The unary * operator can be used to access the value stored at
the address held by a pointer.
int age = 20;
int *ptr = &age;
printf("%d", *ptr);
The expression *ptr accesses the value of age.
The comma operator evaluates its left operand and then its right operand, with the result of the overall expression being the value of the right operand.
int a, b;
a = 10, b = 20;
After this statement, a is 10 and b
is 20.
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", b - a);
printf("Multiplication = %d\n", a * b);
printf("Division = %d\n", b / a);
printf("Remainder = %d\n", b % a);
return 0;
}
= with ==.& when logical AND && is
intended.| when logical OR || is intended.#include <stdio.h>
int main()
{
int a = 10;
int b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
printf("Sum = %d\n", sum);
printf("Difference = %d\n", difference);
printf("Product = %d\n", product);
printf("Quotient = %d\n", quotient);
printf("Remainder = %d\n", remainder);
if (a > b && b > 0)
{
printf("Both conditions are true");
}
return 0;
}
This program demonstrates arithmetic, relational, and logical operators together.
++ increases a value by one and -- decreases it by one.&&, ||, and
!.condition ? value1 : value2.sizeof determines the size in bytes of a type or object.& can obtain an object's address and unary *
can dereference a pointer.Question: Which operator is used for addition in C?